• Post Reply Bookmark Topic Watch Topic
  • New Topic

inner class  RSS feed

 
cybel sheriden
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
page 483 of k&b Q8 of self test, the {} after the new Bar()in the makebar()method is not required , am i correct in saying that.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the code?

Braces following a constructor call denote an anonymous inner class (and since your topic is "inner class," I'm guessing that's the case here), but I would need to see more detail to give a better explanation.
[ February 25, 2005: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it. See this post...

http://www.coderanch.com/t/243357/java-programmer-SCJP/certification/Inner-Class-page
 
cybel sheriden
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i did go through the explaination, this is my understanding

line1 in the given code is as good as putting it as following
void makebar(){
Bar a =new bar(){

};
a.go();
}

in which case the makebar method creats method local anonymous subclass of Bar,which calls the inherited go()method of the superclass(Bar).

iam i correct in saying so.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parentheses allow this new object to be dereferenced without assigning it to a variable.

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

Otherwise, yes, the same thing is accomplished with...

Bar a = new Bar(){};
a.go();
[ February 28, 2005: Message edited by: marc weber ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!