wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Again reachable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Again reachable " Watch "Again reachable " New topic
Author

Again reachable

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Nov 18, 2006
Posts: 689

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

Joined: Jul 09, 2001
Posts: 1176
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

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
subject: Again reachable