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
posted
0
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?
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: 1123
posted
0
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.
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.