"A static method can't access a nonstatic (instance) variable, because there is no instance!"

The above sentence was culled from one of numerous books that i have consulted/study so far on Java,but till now am still having problem with my understanding about that so called "static modifier"
Because with the above statement yet main method which is also static method on which the execution of the whole Java program code depends on still make use of non-static variable when found in the body of code, how come is like that?
Or let say this is one of such problem am yet to understand about this static method of a thing.
Please help me out in breaking this thing down maybe in a more simpler way,probably with simple example to back it up
Thanks to u all


Edited by gyno: spell checking

5 Years
Discussion Span
Last Post by JamesCherrill

A static variable has exactly one value, which is associated with the class itself. To access the variable you just need the class name.
A non-static ("instance") variable has one value for each instance of the class that has been created. To access any one of those values you need the instance that it belongs to.
Similarly, a static method executes within the class itself, and only has direct access to the static variables or methods. Instance methods execute in the context of a created instance of the class, and have direct access to the corresponding instance variables' values.
A static method can't access any of the values of an instance variable unless it has an instance through which to access it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.