Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Example of anonumous class

 
Jayavardhan geepi
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can anybody give me simple example of anonymous classes in java?
Example with custom type is appreciated. Thanks...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayavardhan geepi wrote:Hi, can anybody give me simple example of anonymous classes in java?

first[google it],come up with some example yourself .
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was a thread about that on JavaRanch yesterday, I think in Java in General.
 
Jayavardhan geepi
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is independent anonymous class possible? I mean , an anonymous class that neither extend any class nor implement any interfaces...

@ Campbell,

There is a thread , but that will not help me.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayavardhan geepi wrote:
Is independent anonymous class possible? I mean , an anonymous class that neither extend any class nor implement any interfaces...

No. In the end there is always java.lang.Object as the root of every class hierarchy. But you can make anonymous subclasses of Object. The only thing is, unless you override equals, hashCode, toString or finalize this anonymous subclass will be pointless. Overriding clone is senseless as the class wouldn't implement Cloneable, and all other methods are final. Without overriding any of these 4 methods your anonymous subclass will behave exactly like Object itself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic