aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Anonymous Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Anonymous Inner class" Watch "Question on Anonymous Inner class" New topic
Author

Question on Anonymous Inner class

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
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.


SCJP 6.0 90%
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
You have to assign your class to a variable to call its method..



Regards,
Raja


SCJP 1.4
mambe nanje
Ranch Hand

Joined: Feb 22, 2006
Posts: 31
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.


Da Clone in programming world
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Anonymous Inner class