ContactUs :

Please send your Questions & Answers or Feedback to "mohan@javabook.org"

Statics ?

Variables and methods marked static belong to the class, rather than to any particular instance. In fact, you can use a static method or variable without having any instances of that class at all. You need only have the class available to be able to invoke a static method or access a static variable. Static variables, too, can be accessed without having an instance of a class. But if there are instances, a static variable of a class will be shared by all instances of that class; there is only one copy.

 

By the way, you don't actually need to initialize a static variable to zero; static variables get the same default values instance variables get.

 

main() is itself a static method, and thus isn't running against any particular

Instance of the class, rather just on the class itself. A static method can't access a

non static (instance) variable, because there is no instance! That's not to say there aren't instances of the class alive on the heap, but rather that even if there are, the static method doesn't know anything about them. The same applies to instance methods; a static method can't directly invoke a non static method. Think static = class, non static = instance. Making the method called by the JVM (main()) a static method means the JVM doesn't have to create an instance of your class just to start running code.

 

You can't access a non static(instance) variable from a static method.

 

The rule is, a static method of a class can't access a non static (instance) method or variable of its own class.

 

The Java language also allows you to use an object reference variable to access a static member.

 

Finally, remember that static methods can't be overridden! This doesn't mean they can't be redefined in a subclass, but redefining and overriding isn’t the same thing.

 

Related Posts Plugin for WordPress, Blogger...
Flag Counter