aspose file tools*
The moose likes Beginning Java and the fly likes call the method of the anonymous class?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "call the method of the anonymous class??" Watch "call the method of the anonymous class??" New topic
Author

call the method of the anonymous class??

Wentao Liang
Greenhorn

Joined: Oct 05, 2007
Posts: 13
To call the methods of the anonymous class ,what should I do??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

The anonymous class need to extend a class, or implement an interface that is in scope. And it is a method of this class or interface that you will be using to reference the anonymous class.

You can't just add a method and be able to call it. The anonymous class definition is not in scope.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
When you declare an anonymous class, you declare that it derives from a class or an interface. Therefore, you can call the methods of that class or interface on an object of the anonymous class, as you would for any other object.



If your anonymous class has any methods that are not part of the class or interface from which it descends, then you will only be able to call them from within the anonymous class.

Note that anonymous classes should not be great big classes anyway. If your anonymous class won't fit on a single editor screen, it's too big, and should be promoted to a proper named class.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Kazimierz Pogoda
Greenhorn

Joined: Jun 17, 2008
Posts: 1
Originally posted by Henry Wong:

You can't just add a method and be able to call it. The anonymous class definition is not in scope.


It is not perfectly true:


This code should do the trick.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I typically use a local class instead when I need to do this.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: call the method of the anonymous class??