Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes How do I call a class from method enclosing class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I call a class from method enclosing class" Watch "How do I call a class from method enclosing class" New topic
Author

How do I call a class from method enclosing class

Bala Raj
Ranch Hand

Joined: Sep 12, 2000
Posts: 75
Hi,
How do I call member inner class (inside a method) from outside?

Thanx in advance.
Bala.

[This message has been edited by Cindy Glass (edited September 28, 2001).]
greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
If you must call bMethod() in you main() method some hacking needs to be done:
You can achieve this by making your local inner class implement an interface or inherit from some 'normal' class.
This is your example modified to show how this is done:
Bala Raj
Ranch Hand

Joined: Sep 12, 2000
Posts: 75
Hi greg philpott,
Thanks for your reply.
Under what condition we have to write a class inside a method?
Can you give some practical examples? What are the advantageous?
Thanks,
Bala.
greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
I do not know of any conditions where a local inner class must be used (a class in a method).
I have never had to use one to solve a problem yet, maybe someone with more Swing or AWT experience might have an example for you . . .
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Bala/greg,
You can use a local inner class if the information you want to the class to represent stays within the method and you don't need it once the method returns.
For example, if I have a method that processes records from a database table containing contact information, and this information won't be needed anywhere else, I can create a local inner class to make holding and sorting the records easier.
-Peter
Bala Raj
Ranch Hand

Joined: Sep 12, 2000
Posts: 75
Thanks Peter and Greg.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I call a class from method enclosing class