Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam

 
Sunder Rohini
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Question24 {
public static void main(String[] args){
Question24 q24 = null;
int i = q24.throwDice(); //line 1
int j = getDice().throwDice(); //line 2
System.out.println("Result: "+i+","+j);
}
private static int throwDice(){
return 1+(int)(Math.random()*6);
}
private static Question24 getDice(){
return null;
}
}
I am unable to understand how getdice().throwdice would work ?
I have been coming across code with only one method to invode with its name.
This is confusing . How the invocation takes place here? Could anybody
enlighten in this regard .
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by
This spares us writing something like:

But the main thrust of the question is not about chaining - it is about this code compiling and running even though the references involved all contain null. This works because the methods are static, and so their calls are resolved (bound) by the compiler based purely on reference type. At runtime, it doesn't matter what the references contain.
[ September 06, 2003: Message edited by: Steve Lovelace ]
 
Sunder Rohini
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic