Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Question on Anonymous Inner class

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

I want to know how to access a method, which is written inside an anonymous inner class.

eg) class xxx {

//Code for class

public void myMethod(){
//ActionListener is an interface
new ActionListener(){
{ //initializer
System.out.println("Inside the Anonymous Inner Class.");
}

public void anonymousMethod()
{
System.out.println("Entered anonymousMethod() inside the anonymous class.");
}

} //Anonymous Inner class Ends
} //myMethod Ends
} //class Ends


How to access the method, anonymousMethod(), so that the, System.out.println("Entered anonymousMethod() inside the anonymous class."); will be printed?

Can any of you ranchers clarify me the same?

Thanks,
Anu.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to assign your class to a variable to call its method..



Regards,
Raja
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I think anonymous inner classes are created by extending the class u are instantiating and if u define any method that cannot be accessed by the reference type then u cant call the method due to the fact that at compile time the methods that can be called on an object is being determined by the reference type.
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Thanks for the clarifications.
So we can create reference variables for the class or interface we are extending while creating anonymous inner classes.
and the method iam writing inside the anonymous inner class are the ones defined in these extending class or interface. In other words i can only override the methods of the extending class or interface? and cannot write any other new methods not defined in the parent class or interface iam extending?

Thanks again,
Anu.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic