Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!