This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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

Question about no of objects & references  RSS feed

 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is a qn from one of famous books:

Code:
Thing item, stuff;
item = new Thing();
Thing entity = new Thing();

Qn: how many objects and references are created.

My answer: 3 objects and 2 references.

Actual answer: 2 objects and 3 references.

Can some one shed a light on this??
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishore, the following is the explanation:

Thing item, stuff;
item = new Thing();
Thing entity = new Thing();

You create an object only when you call the constructor of the class. Hence in the above statements, there are two objects created with the new operator. You create references when you declare the variables. In the above case, there are three references namely item, stuff and entity.

Hope that helps.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nina Binde:
Kishore, the following is the explanation:

Thing item, stuff;
item = new Thing();
Thing entity = new Thing();

You create an object only when you call the constructor of the class. Hence in the above statements, there are two objects created with the new operator. You create references when you declare the variables. In the above case, there are three references namely item, stuff and entity.

Hope that helps.



thanks for that explanation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!