File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How a non-static init blocks can assign values to a static instance variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How a non-static init blocks can assign values to a static instance variables?" Watch "How a non-static init blocks can assign values to a static instance variables?" New topic
Author

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

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86



The output is 34
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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...



OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

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

Joined: Apr 13, 2011
Posts: 206
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

Joined: Apr 02, 2011
Posts: 7
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

Joined: Jul 30, 2010
Posts: 15

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:


OCPJP 6, OCEWCD 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How a non-static init blocks can assign values to a static instance variables?
 
Similar Threads
Two dimensional Array (Strange thing)
error in following code
Can a class has two main's
Default constructor
Help Creating Class