aspose file tools*
The moose likes Beginning Java and the fly likes Error in the SCJP6 Study Guide Mock Exam Question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in the SCJP6 Study Guide Mock Exam Question?" Watch "Error in the SCJP6 Study Guide Mock Exam Question?" New topic
Author

Error in the SCJP6 Study Guide Mock Exam Question?

Steve Marriott
Greenhorn

Joined: Jun 06, 2009
Posts: 15
"...you can still access the other Beta object through the static variable a2.b1 - because it's static."

The book says that a2.b1 still points to the other Beta object after this code runs, but I don't see that a2.b1 was ever assigned to anything. I think it's actually a1.b1 that still points to the Beta object that was previously referenced by variable b1. Do I have it wrong?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Steve Marriott wrote:but I don't see that a2.b1 was ever assigned to anything. I think it's actually a1.b1 that still points to the Beta object that was previously referenced by variable b1. Do I have it wrong?

Partly.

You are right that no assignments have been made through a1.b1. However, b1 is static. That means that there is one single reference for all instances of class A. Therefore, whatever its value is, a1.b1 == a2.b1, because it's the exact same thing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Marriott
Greenhorn

Joined: Jun 06, 2009
Posts: 15
Ah yes. I see that now: a1.b1 == a2.b1 Brilliant! Thanks for the response.

Follow up:
static Beta b1;

Is it fair to say that the line above is a static initializer, even though it has no braces as one would find in a 'textbook' static initializer? If that's true, then this helps me to understand even better that this line executes when the JVM loads the Alpha class, not just when the Alpha class is instantiated.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

That's not an (static) initializer but a static declaration. As such, it doesn't "do" anything other then inform the compiler and the JVM that it's there.
David Houghton
Greenhorn

Joined: Jan 11, 2009
Posts: 19
im i right in saying that although the code 'b1 = null' is executed, it is still accessible as the a2 object still has a reference to b1 because its a static variable?

Also am i right in thinking that b1 would not be eligible for garbage collection until there is no reference to it, ie if 'a2 = null' was executed?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

David Houghton wrote:im i right in saying that although the code 'b1 = null' is executed, it is still accessible as the a2 object still has a reference to b1 because its a static variable?


Correct... Setting b1 (the local variable) to null just removes one reference to the object. It is still accessable via the b1 (static variable) of the alpha class.

David Houghton wrote:Also am i right in thinking that b1 would not be eligible for garbage collection until there is no reference to it, ie if 'a2 = null' was executed?


No. Setting a2 to null, will *not* make the b1 (static variable) object eligible. Even if a2 is equal to null, you can access static variables with it, and not get a null pointer exception.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Houghton
Greenhorn

Joined: Jan 11, 2009
Posts: 19
got it, static variables are accessable whether or not a class object has been initialised, or declared for that matter, depending on the access modifiers?

ie method arguments such as 'Color.GRAY'
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in the SCJP6 Study Guide Mock Exam Question?