Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How a non-static init blocks can assign values to a static instance variables?

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output is 34
 
Tommy Delson
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my comments below...




The output is 34



Simply the new values over wrote the old values since all instance share the same static members a & b.

If you still doubting go back to Chapter 1 on Declarations and Access Control and reread to reinforce your knowledge and clear your doubt.

Hope it help...


 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had already looked up in the book but I wasn't capable of finding anything about in it.

Chapter 1, explain slightly about static
Chapter 3, Neither Initialization Blocks example nor Variable Scope cover this case



Thanks you very much, you help me a lot.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, I'm glad that you have grabbed the concept.

The book doesn't provide all the information in details, we need to step out of the box and dig deeper...

 
Yui Huang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen some mock exam question about this. The following code would throw java.lang.NullPointerException.


And the following throws java.lang.ExceptionInInitializerError when the class is loaded.
 
Martin Petransky
Greenhorn
Posts: 15
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI.. in both cases you are trying to change state of a non-existing object. As the array was never created (initialized if you like) you are actually saying:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic