Tuesday, May 17, 2011

More about Static variable & Static method

Static variables & methods are instantiated only once per class. In other words they are class variables,   not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class.

Static methods can be referenced with the name of the class. It may not access the instance variables of that class, only its static variables. Further it may not invoke instance (non-static) methods of that class unless it provides them with some object.

--> When a member is declared a static it can be accessed before any object of its class are created.
--> Instance variables declared as static are essentially global variables.
--> If you do not specify an initial value to an instance & Static variable a default value will be assigned   automatically.
--> Methods declared as static have some restrictions they can access only static data, they can only call other static data, they cannot refer this or super.
--> Static methods cant be overriden to non-static methods.
--> Static methods is called by the static methods only, an ordinary method can call the static methods, but static methods cannot call ordinary methods.
--> Static methods are implicitly "final", because overriding is only done based on the type of the objects
--> They cannot refer “this” are “super” in any way.

No comments: