• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about no of objects & references

 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic