Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Garbage Collector?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have doubt on this question

In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:

1: public class Test {
2: public static void main (String args []) {
3: Employee e = new Employee("Bob", 48);
4: e.calculatePay();
5: System.out.println(e.printDetails());
6: e = null;
7: e = new Employee("Denise", 36);
8: e.calculatePay();
9: System.out.println(e.printDetails());
10: }
11: }

answer : Line 7

Why its line 7 not line 6?

Thanks,

Ricky
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the memory of an object will be eligible for garbage collected ,when ever it was unreachable by any code NOTE: the point eligible . here in the 3rd line ur intiating the object for the Employee class
Employee e = new Employee("Bob", 48);

which was refered to e
inside the method main() so it was avail for that method scope that is upto line no 10 but u r again reiniate the reference e in the line 7
e = new Employee("Denise", 36);

so here the old reference will be unreachable by the code .. so in this line object e old reference will eligible for garbage collected and it will refer to the new memory ...
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi prasad,

I am not cleared abt this doubt. you are right when line 6 is absent. After execution of line 6, the reference varible doesn't point to the real object.
It is not pointing to the object,then why it is not garbage collected?

Regards
krishna.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..

In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected...



Here consider the term earliest...

At line 6 ...you are making the dereferencing of the object..and only after the completion of line 6 ....will the obejct be eligible for GC..
i.e. when it reaches line 7.

Not when you reach line 6..

Regards
[ November 18, 2005: Message edited by: A Kumar ]
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you kumar

Regards
krishna
 
Ricky Ignatius
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by A Kumar:
Hi..



Here consider the term earliest...

At line 6 ...you are making the dereferencing of the object..and only after the completion of line 6 ....will the obejct be eligible for GC..
i.e. when it reaches line 7.

Not when you reach line 6..

Regards

[ November 18, 2005: Message edited by: A Kumar ]



Oh ic, thanks.
Quite tricky question, need to read very very carefully


Ricky
 
Seriously? That's what you're going with? I prefer this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic