File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can you have 2 inner classes ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you have 2 inner classes ?" Watch "Can you have 2 inner classes ?" New topic
Author

Can you have 2 inner classes ?

Jerry McClain
Greenhorn

Joined: Jul 15, 2002
Posts: 5
I am writing a java application and trying to implement event handling. I'm trying to implement a WindowsAdapter and a ActionListener. Since I'm already extending JFrame for my Java app, I can't extend the WindowsAdapter. So I decided to create inner classes for the WindowsAdapter and the ActionListener. My question is: is it possible to have two inner classes within an eclosing class ? If not how do I get around my problem of trying to implement WindowsAdapter and ActionListener ?
Thanks
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
You can have multiple inner classes. It is pretty common to make your action listeners anonymous inner classes. Each component on your frame would then have its own inner class.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Yes, you can have two inner classes, but unless I'm off the mark, this hint might turn a light bulb on:

An inner class can extend other classes (namely, WindowAdapter)
Jerry McClain
Greenhorn

Joined: Jul 15, 2002
Posts: 5
Are you sure about that Mike ?
I tried extending the the WindowAdapter from the inner class, but then the ActionListener method does not seem to be recognized. When I try to compile my code, its states the actionPerformed method cannot be found even though its within the inner class. Are you sure you can extend a class from an inner class ?
Thanks
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Did you declare that the inner class implements ActionListener (as well as extending WindowAdapter)? It doesn't matter whether the class is an inner class or outer - it can extend one other class, and implement as many interfaces as you want.


"I'm not back." - Bill Harding, Twister
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi Jerry, yup I'm sure.

There are two ways, and I'll give you both:(the ....'s imply "more code here"). That's the anonymous inner class way of doing it. This is useful when there is only one time that you would add the listener (which is usually the case). If you had a bunch of 'things' that you need to attach the listener to, then you might declare it as a 'named' (non-anonymous) inner class, and add it a bunch of times. Something like: Here we have the inner class extending the convenience class, and the outer class uses it just once. It's hard to imagine using it more than once for this *particular* example, but the point is, you could.

hope that helps you out.
[ July 16, 2002: Message edited by: Mike Curwen ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you have 2 inner classes ?