« Software Campfire Stories: HashMap Ate My Objects (Java) | Main | What Will This Java Code Print ? »



Feed You can follow this conversation by subscribing to the comment feed for this post.

Sai Matam

Piotr - Thank you for illustrating this concept using byte code.

Yagiz - Thank you for your comments.

Sai Matam.


As you mention at the end of the article, static attributes should be accessed using the class name. Using the instance name is allowed but in my opinion it shouldn't be. However I must admit that your post describes an interesting way showing the difference between the instance scope and the class scope.

Piotr Nowicki

Great example!

In the bytecode (javap -c NullTest) you can see:

12: getstatic #5; //Field count:I
15: iconst_1
16: iadd
17: putstatic #5; //Field count:I
20: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;

So the compiled code is referring to the static field, omitting the instance of NullTest.


The comments to this entry are closed.