posted 18 years ago
Non-static variables are associated with instances of a class. So in your program, every instance of testing has its own a and b. In a non-static method, the statement
is an abbreviation for
In other words, the statement sets the value of x of the current object.
In static code, you aren't dealing with individual objects/instances. There is no current object. So when your method1 uses the variable , the compiler doesn't know which "a" you meant.
Static methods may not read/write non-static variables, and may not call non-static methods, unless they explicitly specify which object owns the var or the method. SO the following would compile ok:
Hope this helps.
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".