jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes What does this reference mean?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What does this reference mean??" Watch "What does this reference mean??" New topic
Author

What does this reference mean??

Ravissant Markandey
Ranch Hand

Joined: Oct 24, 2007
Posts: 41
Hi everyone,
I came upon this piece of code in Kathy sierra, wasn't able to make much sence of it so thought of asking, could anyone tell me why SOP(1) gives an object while SOP(2) gives null, actually the piece of code:instance1.instance2 is not clear to me, the code is as follows:




Thanks in advance,
Ravissant Markenday
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
q1 was created with the no argument constructor of the Q6 class, which doesn't initialise its qInstance variable.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Q6 q1 = new Q6(); calls the first constructor, which does nothing. As such, q1.qInstance never gets anything assigned to it.

Now the rule is, any field (not local variable!) that never gets a value assigned will get a default value - null for objects. Since q1.qInstance never gets anything assigned to it, it will remain null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ravissant Markandey
Ranch Hand

Joined: Oct 24, 2007
Posts: 41
gawd yea ..Thanks so much Joanne
Ravissant Markandey
Ranch Hand

Joined: Oct 24, 2007
Posts: 41
Yes Rob I got it, thanks so much
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What does this reference mean??
 
Similar Threads
How to use remove() method on an ArrayList
Gradebook
doubt regarding a question abt garbage collections K&B 3rd chapter
math program help..
Doubts: Security, Related Question - Help