• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

call the method of the anonymous class??

 
Wentao Liang
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To call the methods of the anonymous class ,what should I do??
 
Henry Wong
author
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kazimierz Pogoda
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I typically use a local class instead when I need to do this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic