This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return triggers method end but overriden?

 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, in the below code is a question from Marcus Green Q's on Examulator..
public class Phr4jr{
private Phr4jr(){
System.out.print(1);
}
public static void main(String argv[]){
Phr4jr ph = new Phr4jr();
System.out.print(ph.go());
}
public int go(){
try{
System.out.print(2);
return 1;
}finally{
System.out.println(3);
return 2;
}
}
}
What is the output?
Well, i guessed it right and chose 1232 ... guessed meaning, i do not really understand why it isn't 1231 - the issue being the last number that is printed, is the number that is returned, and the first return statement's arg is 1...the return statement says "End of method - go back!", to which the method says "Oh yeah? well first let me do 'Finally!'" and then finally has a return in it which gives 2 as an arg.
so 2 is the last number, is that why?
thanks in advance
 
Dan Culache
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jasper,
I guess you got it right. To make the case stronger I tried :
 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan, the code gives a compiler error of 'unreachable statement' for the 3rd return statement, but i get the general idea
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic