• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in garbage collection

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I had Registered here two moth ago but this is my first post in this forum.

I m preparing for scjp 6 again. yes i had tried it once on Jan 29 (because my voucher is going to expire on jan 31) but failed (51%).

now i am going again for scjp and want to clear this time with 90+.

I had one doubt about the question in devka's diagnostic exam:




Question asked in the mock was "How many objects are eligible for GC after line 12 ?" .

And the answer is "1".

my doubt is that when creating object in the above question, null passed as argument to constructor .

will object be created on the heap (having "null" as value) this way ?

what i thought that there is no object created at all.........i was wrong but need explanation on this. please help.

and please forgive me for my grammatical mistakes......







 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try searching next time.
A thread about this question was posted only two days ago:
https://coderanch.com/t/442005/Programmer-Certification-SCJP/certification/many-object-eligible-garbage-collection
 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Kieren but the link is pointing to a different question. I also have the same doubt as Ravi that whether a new object will be created with a null reference or no object is created?
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check if some one already raised in the forum when you send any question
 
Kieren Dixon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, they looked very similar.

what i thought that there is no object created at all.........i was wrong but need explanation on this. please help.


You are correct, not wrong.
You have probably gotten something else wrong along the way and confused yourself.



So as you can see, there is only 1 object which does not have a reference
 
Ravi Keshare
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you all for replying.

Kieren you explained with great efforts . what i got from it that whenever you say "new" an object is created on heap.
In above question "null" passed as argument to constructor, is actually assigned to an internal object
and this object is the property of an outer object .so outer object has a proper state which proves its existence.
please correct me if i am going wrong.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ravi Keshare wrote: In above question "null" passed as argument to constructor, is actually assigned to an internal object
and this object is the property of an outer object .so outer object has a proper state which proves its existence.
please correct me if i am going wrong.


Yes, but don't use inner/outer object names, people may gets confused it with the inner/outer class. You can say like, "A class contains an instance of other(same) class" OR A HAS a A.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i really am still unable to understand as

a1 ----> null

a2 -----> new A() ----> new A() ----> null meaning
a2 -------> null

a3 -------> new A ---> new A() -----> new A() ----> null
meaning
a3 --------> null

a1 = a3

a1.a = null here it should lead to NullPointerException coz a1.a is not reachable coz a1 ------> null

 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic