Net-informations.com
SiteMap  | About    

Static Method vs Non-Static Method

A static method belongs to the class and a non-static method belongs to an object of a class. The static methods can by accessed directly from the class, while non-static methods (or instance methods as I like to call them) have to be accessed from an instance.

An example would be the static method "Show" from the static class MessageBox . When you need a messagebox, you just call a static method to show it.

Example

If it weren't static, you would first have to create an instance of the class that contains it it would be like:

A static method is shared by all instances of the class. Whenever a method is called in C++/Java/C#, an implicit argument "this" reference is passed along with/without the other parameters. In case of a static method call, the "this" reference is not passed as static methods belong to a class and hence do not have the "this" reference.

A non-static method can only be called on an object of a class that it belongs to. A static method can access only static members. A non-static method can access both static and non-static members because at the time when the static method is called, the class might not be instantiated (if it is called on the class itself). In the other case, a non-static method can only be called when the class has already been instantiated.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.