• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

What does this reference mean??  RSS feed

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
q1 was created with the no argument constructor of the Q6 class, which doesn't initialise its qInstance variable.
 
Sheriff
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravissant Markandey
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gawd yea ..Thanks so much Joanne
 
Ravissant Markandey
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Rob I got it, thanks so much
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!