Tuesday, May 20, 2008

Static variables and methods

‘static’ is a modifier in java which can be used for methods and instance variables and a block of code outside any methods inside a class.
We can use static as a modifier to a class or a method when the member’s behaviour doesn’t depend on the instance of the class so that there will be no need to create an instance of that class to access the static members.

Variables and members marked as static belong to a class rather than any particular instance of the class. So, if you modify a variable in one instance of the class, it will affect all future use of the variable in any other instance of the class (unless the class itself is reloaded).

Static methods can’t access non-static variables of a class.

Static methods cannot be overridden.

Static modifier can only be applied to methods, instance variables and top level inner classes.

No comments: