• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Why anonumous inner class is not take the ";"

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is derive from SCJP5.0 K&B book.
Given:
1 public class Foo{
2 Foo(){System.out.print("foo");}
3 class Bar{
4 Bar() {System.out.print("bar");}
5 public void go(){System.out.print("hi");}
6 }
7 public static void main(String[] args){
8 Foo f = new Foo();
9 f.makeBar();
10 }
11 void makeBar(){
12 (new Bar() {}).go();
13 }
14 }

What is the result?
A. Compilation fails.
B. An error occurs at runtime.
C. foobarhi
D. barhi
E. hi
F. foohi

The answer is C. foobarhi

My question is;
At the line 12 why it not written to (new Bar() {};).go();
I think anonymous inner class must follow by ';' in the plain-old one and follow by ');' for argument anonymous. But, why this given code is not follow this rule.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tanakorn,

I think anonymous inner class must follow by ';' in the plain-old one and follow by ');' for argument anonymous. But, why this given code is not follow this rule.


neither of the two cases you described applies here,in the above code a mehtod is being invoked on the anonymous class object.
[ January 09, 2007: Message edited by: Sanjeev Kumar Singh ]
 
Tanakorn Numrubporn
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for reply
Can I make a conclusion that when we invoking method for Anonymous object, it not neccessary to add ';' behind '}' of that Anonymous object?
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More than necessary. It is mandatory that you don't put the semicolon.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The semicolon simply marks the end of a statement, as always. It sometimes looks unusual in the context of an anonymous class, but it's not really any different.

For example, an anonymous class is often defined in a simple assignment statement, where the right side is basically a constructor call with the anonymous class body slipped in after the parentheses. But this is still just a statement, so it ends with a semicolon.

MyClass mc1 = new MyClass();
MyClass mc2 = new MyClass(){}; //anonymous body slipped in

It's also possible to create a new instance -- without assigning it to a variable -- for a one-time method call. An anonymous class body can be slipped in here as well. Of course, this is still just a statement, so it ends with a semicolon.

(new MyClass()).method();
(new MyClass(){}).method(); //anonymous body slipped in
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic