• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Anonymous classes

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have these two classes,
My Question is how do i access the foo method in the anonymous class? Its not getting invoked!!or should i have to make it a local inner class?
<pre>
public interface MyTest
{
public void foo();
}

public class Outer
{
public void aMethod()
{
new MyTest()
{
int i =10;
public void foo()
{
System.out.println("Success" +i);
}
};
}
public static void main(String args[])
{
Outer o = new Outer();
o.aMethod();
}
}
</pre>
In case of anonymous eventlisteners, how does the public void ActionPerformed(ActionEvent e) method get executed when there's no explicit call to it?
[This message has been edited by Paul Wheaton (edited August 20, 2000).]
 
Trailboss
Posts: 23242
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you don't mind that I added a little indenting so I could read your sample.
You have made an anonymous class. To access the foo method, you would need to pass your anonymous class to something or make some sort of public reference to it.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding anonymous inner classes as event listeners, in code such as the following:
Button b = new Button( "Pick me" )
b.addActionListener( new ActionListner() {
public void actionPerformed( ActionEvent e ) {
System.out.println( "You picked me" );
}
});
The addXxxxxListener() call stores a reference to the anonymous listener in the component (ie Button b). Then when an XxxxEvent occurs on the component, the event is passed to the registered listener's event method (ie actionPerformed()) using the reference stored in the component.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To put it to work, I just posted an example code in this
msg
. check it out.
Hope it helps.
Regds.
- satya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
How should i do that. can u please elaborate?
reply
    Bookmark Topic Watch Topic
  • New Topic