• Post Reply Bookmark Topic Watch Topic
  • New Topic

another GC question  RSS feed

 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. can a static method be declared from within another?
e.g.main method - back up ur ans. with reasons.
2.55. 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; //to me e that holds Bob and
//48 becomes eligible for gc here,
7. e = ne Employee("Denise", 36);//while the
// e here references the new objects "Denis" and 36
8. e.calculatePay();
9. System.out.println(e.printDetails());
10. }
11. }
1.Line 10
2.Line 11
3.Line 7
4.Line 8
5.Never
Answer is 1 i.e. line 10 but i would say 3 i.e. line 3 because
the questions used the word "originally"?.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't declare a method within a method.
I'm making the assumption here that you are referring to a method inside a method, not a method inside a method-local inner class (a class declared inside a method). If that assumption is incorrect, please clarify.
Thanks.
[ October 09, 2003: Message edited by: Jeff Bosch ]
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the other question, on first blush it looks like the original object is eligible for gc after line 6, as you say.
But, can you guarantee me that calculatePay() and printDetails() do not pass a reference to the object to another object? Without knowing exactly what those methods do, you can't say for certain that the original "Bob" object is no longer referenced and therefore eligible for gc.
For example, what if printDetails passes a reference to "this" to a print queue, then returns? The object still exists, still has a reference, and therefore cannot be gc'd.
At line 10, the program terminates. All objects created by the program and not serialized are then eligible for gc.
Hope that helps.
[ October 09, 2003: Message edited by: Jeff Bosch ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static method "can" be declared within another method, just not directly.
i.e. a class can be declared within a method, and a method can be declared within a class, therefore, transitively speaking, "a method can be declared within a method".
As for the next question, this is just (yet another) case of an ambiguous question with an indeterminate answer - you will not find such silly questions on the exam.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Tony in that that the question is ambiguous. It's very hard to say at what point the object referenced by e is available to GC if at all.

But, can you guarantee me that calculatePay() and printDetails() do not pass a reference to the object to another object?

I don't see this particular case from the signature, however, nobody can guarantee that calculatePay() doesn't create a new thread possibly running even after main() completes and holds a lock on e object. As we know, an object is available to GC only when no live thread can access it.
 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks y'll,
i also thought the question was a little too ambigous.
its a good feeling to know that sun doesnt ask such ambigous questions.
cheers.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!