• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

chapter 3 self test question 10, K&B

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why are Five objects were created and Two objects are eligible for GC???
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mauricio. If you search the forum first, you'll find a lot of threads discussing the same problem.
 
Mauricio Archbold Babroza
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
corret me if i wrong,
Dozens [] da = new Dozens[3]; --> 1 object
da[0] = new Dozens(); 2 objects , one is the object dozens and the other is an array of int dz
Dozens d = new Dozens(); 2 objects , one is the object dozens and the other is an array of int dz
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes your interpretation is correct.

Earlier you might have missed the array. Array of Dozens is also an Object.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yes there are two objects for garbage collection.



If you see this snippet taken from the code, here first a object of dozens was created and was referenced
by a reference d. Then da[1] also started pointing towards the same dozens object after statement 2. Now
remember that, when you create the the dozens object another array object is also created which is called
da.

If you observe statement 3 and 4 then you can find out now both the references which were pointing towards
the Dozen object has been set to null, so there is no way to access that object nor any alive reference to da,
so now the dozen object and array object both are eligible for garbage collection.

Best Regards,
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
made a rough drawing in paint , so that it will be easy to imagine which objects are going to be available for GC!

 
Bartender
Posts: 2653
18
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi "De Coder",
Please check your private messages for a JavaRanch administrative matter.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

De Coder, please change your display name to point towards your original name. It is against
the policy of java ranch to put fake names for display.

Best Regards,
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!