wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class question 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 "Inner class question" Watch "Inner class question" New topic
Author

Inner class question

Kannan Raju
Ranch Hand

Joined: Jul 07, 2005
Posts: 44


My question for the above code is, what is the significance of {} at the place I marked //here. It is not creating an anonymous class. It is printing the same result with the {} and without the {}. I think it has been place there to confuse us. Please let me know. Thx.


SCJP1.4<br />SCWCD (in progress)
samdeep aarzoo
Ranch Hand

Joined: Jun 09, 2005
Posts: 160
firstly it is not an anonymous class

as in anonynous class
code must be like this new Bar(){};
In anonymous class semicolon must be after the second curley braces
and here it is (new Bar(){}).go();


It is printing the same result with the {} and without the {}


and about this u can write anywhere {} in code
this will compile as comiler check matching braces
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by bikash mittal:
firstly it is not an anonymous class

as in anonynous class
code must be like this new Bar(){};
In anonymous class semicolon must be after the second curley braces
and here it is (new Bar(){}).go();


That's incorrect. new Bar() {} is an anonymous class. The semicolon does not need to be immediately after the declaration of the anonymous class. What is true is that a statement where an anonymous class is created and instantiated must end in a semicolon, which this statement does. However, that does not mean that the semicolon must occur immediately after the anonymous class declaration.

(new Bar() {}).go();

That statement ends in a semicolon, as it should.

The significance of the {} in that statement is that it tells the compiler to create an anonymous class that is a subclass of Bar, and to instantiate that anonymous class. If you wanted to, you could have added code inside the braces to alter the functionality of the anonymous class. For instance, you could have overridden the go() method in the anonymous class like this:



The reason that the behavior of

(new Bar() {}).go();

matches the behavior of

(new Bar() ).go();

is that the anonymous class you created does not implement any changes from its superclass, Bar. The anonymous class inherits the public method go() from Bar, and therefore the program produces the same output in both cases. Try overriding the go() method inside the braces to see a change take place in the output.


SCJA 1.0 (98%), SCJP 1.4 (98%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class question
 
Similar Threads
Nested classes
another question on inner classes
Inner Classes
Inner Class k&B page 483
K&B chapter 8 Self Test Q 8