wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why is this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is this?" Watch "why is this?" New topic

why is this?

Holmes Wong
Ranch Hand

Joined: Feb 18, 2002
Posts: 163
why the following code prints out "Sample", could you explain the strange code line at (1). Thanks.
chafule razgul
Ranch Hand

Joined: Feb 09, 2002
Posts: 63
if you want to refer to a class variable via the class name, eg Test4.something then by definition something is a class variable. But since flag is an instance variable (non-static) you can still refer to it via the class name, except that you need to flag it with this, as in the example, Test.this.flag
Hope this helps
Holmes Wong
Ranch Hand

Joined: Feb 18, 2002
Posts: 163
Thanks. My question is about the following snippet actually:

You see a semicolon after if statement, and sample
method is inside a pair of braces. Is this allowed inside inner classes?
Padma Shyam

Joined: Feb 14, 2002
Posts: 7
if (Test4.this.flag);
The variable flag of the outer class is referred inside the inner class by Test4.this.flag.
Since there is a semicolon at the end of the 'if' statement,the if condition ends there.
The next statement is the call to the sample() method.So,it displays Sample.If you remove that semicolon and run the program,the boolean condition of 'if' is false and so there is no output.
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
You can have block statements in a method/inner class/top-level class ...etc :
BTW - { ... } is a block statement.
void foo() {
System.out.println("I'm in a block haha");
There is nothing wrong with it.
I agree. Here's the link: http://aspose.com/file-tools
subject: why is this?
It's not a secret anymore!