• 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

please look at it!!

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the following code is about a inner class, the right output is "Sample", it make me confused, i think it should be nothing,
please help me!!
-----------------------------------------------------------
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();
}
}
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because you have a semicolon at the end of your if statement .

Originally posted by David chenjl:
the following code is about a inner class, the right output is "Sample", it make me confused, i think it should be nothing,
please help me!!


 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what happens:
The constructor is called from main
The constructor calls superclass contructor
Initializers are executed (flag is set to true)
The rest of the constructor is executed
So, call to sample() occurs after flag is set to true.
 
Alas, poor Yorick, he knew this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic