• 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

Anonymous Inner class

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Inquisition



Line 1 the anonymous class syntax is right?? I mean, there is no semicolon, but still compiles fine.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the syntax is correct as when you define an anonymous inner class as method invocation argument, the class definition has to be ended with a closing curly brace only.
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you are declaring an inner class there is no need to add a semicolon. however when you are instantiating an object using an anonymous class then you have to add the semicolon at the end.

if you take this example you should understand the whole story ;)

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You will see a lot of questions on Anonymous Inner classes, so you"ll have to be very picky about the way they"re closed. If they are argument local, they end like this: });

but if they are plain-old anonymous classes, then they end like this:
}; K&B




:?:
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes anonymous classes are the ones you use to extend/implement a class/interface and then override the needed methods and then create an object from it, these are the anonymous classes, which ends with a semi-colon -;- which is the case in inner2.

however Inner1 is not an anonymous class, its just a method local class since its definition is inside a method, there is the other type which is inner class which is declared inside another class. but those are not anonymous inner classes. the anonymous inner classes are used as I mentioned in the first paragraph.

got it ???
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Here the class inside seth is an anonymous class. Why isnt there a semicolon??
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
seth( new face(){});

This is a complete statement. You can't add two semicolons to a statement. If you write this

seth( new face(){}; );

Then it would be an invalid syntax...

[Edit: removed emoticon from code ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think a semi-colon is required here.


Abhi vijay wrote:


Here the class inside seth is an anonymous class. Why isnt there a semicolon??

 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because its passed as a parameter to a method .. where you find an anonymous class replace it with an object and you will understand wether you need a semicolon or not.

would you put a semicolon after a parameter in a method ?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi it's not mandatory to put a semicolon after an anonymous inner class in every case. It's needed if you are creating the anonymous inner class and doing nothing else in the statement. Suppose you write this

new Runnable(){public void run(){}}.run();

Now in this case too you won't write it like this

new Runnable(){public void run(){}};.run();

So basically a semicolon is required if the anonymous inner class creation statement contains only the creation of the anonymous inner class and no method calls.

(Now I know that the code that I wrote will not start a new thread so don't remind me that :lol: )
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi, just remember one thing, you have to put semicolon at the last of the expression or statement, why do you want to put it in between the expression.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just forget about the inner class thing for a while and look at this:

At // 1, semicolon at the end of the assignment statement. At // 2, no semicolon at the end of the assignment statement which is inside the full statement (method call.)
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic