To err is human
Even the SCJP examiners overlooked the following:
Like wha? I was totally troubled after reading the code above. How come the static methods are able to access the instance (non-static) variable? I re-read the previous chapter and typed and compiled the code on my own.
% javac Foo.java
Foo.java:11: non-static variable size cannot be referenced from a static context
System.out.println("size = " + size);
Foo.java:12: non-static variable size cannot be referenced from a static context
changeIt(size);
Foo.java:13: non-static variable size cannot be referenced from a static context
System.out.println(”size after changeIt is ” + size);
3 errors
Thankfully, it didn’t compile. No debunking of previous “theories”. It was only a typo.
The variable should be declared with a static modifier.