Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

; after the if statement

 
santhy sugathan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to this forum and got a question. I saw this question in the JDcert mock exam and got a doubt.
In this code, what will be the output? I tried tu run and got "Sample" as the output. But why the compiler passes this statement "if (Test4.this.flag); ", ie, if ending with a semicolon?
public final class Test4 {
class Inner {
void test() {
if (Test4.this.flag); {
sample();
}
}
}
private boolean flag = true;
public void sample() {
System.out.println("Sample");
}
public Test4() {
(new Inner()).test();
}
public static void main(String args []) {
new Test4();
}
}

Somebody pls help me.
Thanks
Santhy
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi santhy,
Answer to your question is here http://www.javaranch.com/ubb/Forum24/HTML/006278.html
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (Test4.this.flag);
{
sample();
}
The if statement states that if flag is true, do nothing. The semicolon alone is same as an empty statement. The rest is an instance initialization block. Hope this helps.
 
santhy sugathan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nasir and Sam. Really apperciate it.
Santhy
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic