• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner Class

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one explain, why the following code prints "Sample". Thx.
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();
}
}
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason it prints sample is because your if statement doesn't check for anything and allows the sample method to be processed. Try this..

public final class Test4 {
class Inner {
void test() {
if (Test4.this.flag == true) {
sample();
}
else {
System.out.println("The flag is false");
}
}
}
private boolean flag = false;
public void sample() {
System.out.println("Sample");
}

public Test4() {
(new Inner()).test();
}
public static void main(String args []) {
new Test4();
}
}
[ May 29, 2003: Message edited by: Shane Hartman ]
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm.. actually the semi-colon after the "if()"
if (Test4.this.flag); {
should be the one that is causing the problem (or maybe it was meant to be there to check - eye for detail !) . Try removing and running the ";" after the if().
 
reply
    Bookmark Topic Watch Topic
  • New Topic