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

Please Explain

 
vinay jain
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pre>
public final class Test4 {
class Inner {
void test() {
if (Test4.this.flag); {
sample();
}
}
}
private boolean flag = false;
public void sample() {
System.out.println("Sample");
}
public Test4() {
(new Inner()).test();
}
public static void main(String args []) {
new Test4();
}
}
</pre>
In the above code the answer is printed "Sample" on the standard output. Can anyone please explain why?
 
vinay jain
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More Info on the mentioned Question. It is from John Hunt's Mock Exam, Question 62?
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a trick question. Notice the ";" after the "if" statement. In this case, there is no action taken as a result of the "if" determination. The program then will simply go to the next line of code: sample().
While this type of question is fair game in the SCJP exam. I didn't have any of these types of trick questions.
 
vinay jain
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darryl
I was confused about 2 things and I noticed the ; after your answer and 1 thing got cleared. The other thing that I was confused about was the use of <OuterClassName>.this.<outerClassMember> but then I read a little bit about it too and cleared that too. Thanks for the timely response and making things clear for me.
Vinay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic