« 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.


Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)