rss search

Static vs. Non-Static Methods

line Static vs. Non-Static Methods

A static method belongs to the class and a non-static method belongs to an object of a class. That is, a non-static method can only be called on an object of a class that it belongs to. A static method can however be called both on the class as well as an object of the class. 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. A static method is shared by all instances of the class. These are some of the basic differences. I would also like to point out an often ignored difference in this context. Whenever a method is called in C++/Java/C#, an implicit argument (the ‘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.

0 comments

line

Trackbacks/Pingbacks

  1. java programming lesson - everything about java... [...]Static vs. Non-Static Methods | Grady Laksmono[...]...
  2. non the thao| non ket nam| mu luoi trai - non the thao| non ket nam| mu luoi trai... [...]Static vs. Non-Static Methods | Grady Laksmono[...]...
  3. middleman - middleman... [...]Static vs. Non-Static Methods | Grady Laksmono[...]...

Leave a Reply

You must be logged in to post a comment.