Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Double K&B Chapter 3- Question 11

 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
This is an exercise question from K&B and i have a doubt.


To my knowledge, there should be two objects eligible for garbage collection. a1 and b1, but book says only one object is eligible for
garbage collection. Exact explanation is written like this

It should be clear that there is still a reference to the object referred to by a2, and that there is still a reference to the object referred by
a2.b2. What might be less clear is that you can still access the other Beta object through the static variable a2.b1, because its static.

Why a2.b1 is an object? It should be a reference variable having null because we never initialized the value of a2.b1. By default when it is
initialized it will be having a value of null.

Please clarify.

Thanks,
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hint: take a look at line 4 of your post.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am unable to understand still why only one object is eligible for garbage collection.
What about objects b1 and b2 in main? Please can someone explain me this problem
and this static reference field. I am confused.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question has been asked many times before. Search the forum for "alpha beta", you'll find discussions like the following

https://coderanch.com/t/481398/Programmer-Certification-SCJP/certification/many-eligible-gc
https://coderanch.com/t/474980/Programmer-Certification-SCJP/certification/Garbage-Collection-Clarification
https://coderanch.com/t/469606/Programmer-Certification-SCJP/certification/Garbage-Collection
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit.

Understood it. Very minor mistake i was making, will take care in future.

Best Regards,
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic