• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flow control

 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At which point will the word "Harpic" be printed out when this code is executied?
class Harpic{
public void finalize(){
System.out.println("Harpic");
}

}

public class ArmitageShanks{
Harpic har;
public static void main(String argv[]){
ArmitageShanks as = new ArmitageShanks();
as.oui();
}
public void oui(){
har = new Harpic();
mno(har);
//one
har=null;

}
public void mno(Harpic har){
Harpic pic=har;
//two
pic=null;
//three
har=null;
}
}



1) After executing the line after the comment //one
2) After executing the line after the comment //two
3) After executing the line after the comment //three
4) It is impossible to say, the String "Harpic" may not be output at all.

ds is a question from Marcus green,culd any buddy would help me to trace the flow thanks
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U cannot say when garbage collector runs and finalize method is called.
So, according to me correct option would be "4"
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4
 
sabeer
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the given code the method "finalize" of class "Harpic" never called from the main method. So choice 4 is correct answer.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the given code the method "finalize" of class "Harpic" never called from the main method. So choice 4 is correct answer.


4 is the correct answer, but not for this reason.
An object will certainly have the finalize() method called when it is collected. An object is not guaranteed to be collected, not ever. Therefore, the finalize() method is not guaranteed to be called. Therefore, 4 is the correct answer.
 
subramanian annamalai
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i do would like to know let as assume gc is getting called , in that case what will be thr output;
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by subramanian annamalai:
i do would like to know let as assume gc is getting called , in that case what will be thr output;



If we assume that after having no reference that object will be collected immediately , then answer would be 1 .

hope I am right .
[ March 20, 2005: Message edited by: rathi ji ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic