File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can anyone explain me this program, which objects are eligible for garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can anyone explain me this program, which objects are eligible for garbage collection" Watch "can anyone explain me this program, which objects are eligible for garbage collection" New topic
Author

can anyone explain me this program, which objects are eligible for garbage collection

soujanya Bugatha
Greenhorn

Joined: May 29, 2012
Posts: 10
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
After line 19, the object created in line 17, A object, is eligible for GC as well as its B object created at line 4.
sarvesh dikonda
Ranch Hand

Joined: Apr 08, 2012
Posts: 58

Himai Minh wrote:After line 19, the object created in line 17, A object, is eligible for GC as well as its B object created at line 4.

i tried to run the same code with a few modifications just to check whether what you quoted was right or not. The code with modifications is as follows:


As in the above code you can spot that i have just commented the line 22 (i.e. aa=null) just to see if the 'aa' is pointing to the same location (at which the object of class A is stored) as 'a' (in class B) is pointing to.

After compiling and running the code the output i got was -

a reference var points to A@3e25a5
b reference var points to B@19821f
In the main() A@3e25a5

From the output it can be seen that object of class A that was created in the main() is still being referenced by 'a' of class B. So, as per my understanding i don't think any of the object is eligible for GC. Kindly, correct me anyone if i am wrong, with a convincing reason.

I am also aware that, if line 2 is changed to-> static protected B b; and if line 11 is changed to-> static protected A a; i.e. if i want to print the values of each of the references in the main() (i.e. if a i insert System.out.println(A.b+" "+B.a); to main()) then, i will get the output as-

a reference var points to A@3e25a5
b reference var points to B@19821f
In the main() null
B@19821f A@3e25a5

As you can see in the above output that i have removed the comment from line 22 (i.e. aa=null). Kindly help me know if i am correct anyone but with a convincing reason.


Always believe in yourself
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
Try to uncomment aa=null and make b non-static and see.
Exam question is : which object is eligible for GC after aa =null?

When b is static, it belongs to the class. Static object is not eligible for GC.
sarvesh dikonda
Ranch Hand

Joined: Apr 08, 2012
Posts: 58

Himai Minh wrote:Try to uncomment aa=null and make b non-static and see.


Now as you said, i tried doing that way as you can see in the code above which i posted earlier

Himai Minh wrote:Exam question is : which object is eligible for GC after aa =null?

When b is static, it belongs to the class. Static object is not eligible for GC.


I know that when b is static what happens that's just the latter part that i included in my earlier post just to say that what happens if a 'static' keyword is put.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

If you wanna learn this then it's fine, although this topic is not part of ocjp exam anymore.

-hth


OCA7
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
For OCJP 6, GC is not in the scope anymore? I'm not sure.

In case the exam requires it, you'll be asked if an object is eligible for GC after a certain line of code. But there is no guarantee this object is GCed. In KB's study guide, there is no guarantee the VM will GC any object until you exit the program.
If an object has no reference refering to it, then it is eligible for GC. But when it is GCed? We never know.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can anyone explain me this program, which objects are eligible for garbage collection
 
Similar Threads
instanceof operator
return super.getC(); equals to return this.getC();
what is the output for this - give explanation
question on NumberFormat class
Polymorphism example - where would we ever use this construct?