• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output is 34
 
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic