• Post Reply Bookmark Topic Watch Topic
  • New Topic

Again reachable

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look a code



Which of the following line can be placed at /*insert here*/ can save the object obj from GC. Specify reason



 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Questions for you to ask yourself:

1. What does the static keyword mean? What is the difference between a static field and an instance field?
2. The garbage collector won't collect an object if there is a live reference to it. Which line would keep a reference to the object alive?
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why K&B makes such a big deal about saving objects from GC. I can't think of any practical reason to save an object from GC.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both compile and seem to run just fine. B is probably better as each instance keeps a reference to the object while in A only the last reference will be saved.

Regards,
Dan
 
Paul Clapham
Sheriff
Posts: 21867
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaydell Leavitt wrote:I don't know why K&B makes such a big deal about saving objects from GC. I can't think of any practical reason to save an object from GC.


You're right about that. However a large number of questions in those certification exams are about things which no sane programmer would ever do. Follow this forum for a while and you'll see what I mean. (Although I haven't seen "i = i++" for a while.) But since Java lets you do those things, and the tests are supposed to be comprehensive, they have to be covered by the tests.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!