Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

doubt regarding a question abt garbage collections K&B 3rd chapter  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the following class; and my doubt is at line 3 how many obj are eligible for garbage collection. The answer is 2( q1 and story) .But at line 1 when q3 =null, means it is also eligible for garbage collection right? Can anyone please help me understand this in a more detailed way.

public class Ques04 {
Short story = 200;
Ques04 Ques04(Ques04 q) {
q = null;
return q;
}
public static void main(String[] args) {
Ques04 q1 = new Ques04();
Ques04 q2 = new Ques04();
Ques04 q3 = q1.Ques04(q2);//line 1
System.out.println(q3);//line 2
q1 = null;//line 3
}
}
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sree,

Please quote your sources. We won't be able to help you until you do so.

Thanks.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sree javab wrote:Hi,
I have the following class; and my doubt is at line 3 how many obj are eligible for garbage collection. The answer is 2( q1 and story) .But at line 1 when q3 =null, means it is also eligible for garbage collection right? Can anyone please help me understand this in a more detailed way.

public class Ques04 {
Short story = 200;
Ques04 Ques04(Ques04 q) {
q = null;
return q;
}
public static void main(String[] args) {
Ques04 q1 = new Ques04();
Ques04 q2 = new Ques04();
Ques04 q3 = q1.Ques04(q2);//line 1
System.out.println(q3);//line 2
q1 = null;//line 3
}
}



q3 is an object reference, not an object, so if it is set to null it doesn't mean there is another object available for the dreaded GC.
 
sree javab
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe, that clarifies my doubt.

Ruben,
I mentioned that my question was from Kathy and BertBates 3rd chapter in the subject line itself. Please look at the subject of the post.

Thanks.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you sure did Sree. I don't know how I missed that...
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DELETED
 
today's feeble attempt to support the empire
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!