File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner Classes" Watch "Inner Classes" New topic

Inner Classes

Martin Rennix
Ranch Hand

Joined: Sep 30, 2001
Posts: 34
I am having great difficulty understanding how and why inner classes are used. I understand they are important in Swing programming but apart from that, are they very important or can I get by without knowing them in detail?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

You can get by without knowing them in detail. (They are just a simple way of hiding a class inside of another class.)

Please ignore post, I have no idea what I am talking about.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Well, I would tend to disagree. In the real world there is a HUGE use of inner classes, and the topic is covered on the Cert Exam.
You really need to read Getting in touch with your inner class
[This message has been edited by Cindy Glass (edited October 24, 2001).]

"JavaRanch, where the deer and the Certified play" - David O'Meara
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I'd agree with Cindy, also. I use them anytime that a class has a need, not just with Swing. Quick arbitrary example, Fruit and Seed. Since all fruits contain seeds, and seeds come from fruits, it makes sense to put Seed as an inner class of Fruit. You wouldn't make seed it's own class because you would never be able to instantiate a seed without a fruit, same reason you wouldn't make it a static inner class. There's plenty of reasons why you would use an inner class, Swing is just one of them. If you have the hang of using inner classes from doing a bunch of applets, though, you at least have the basics down and should be able to handle them.
Enough about produce
Martin Rennix
Ranch Hand

Joined: Sep 30, 2001
Posts: 34
Ok, I admit I have just read the "Inner Classes" chapter from Eckels's "Thinking in Java" and it kinda makes more sense.
I guess I just have to play around more with examples to show off their usefulness. I did quite like Eckel's explanation of a Controller class that handled events using inner classes. The advantage seemed to be that inner classes could be declared private and hence completely hide the implementation of an interface.
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
I have used inner classes very little since my journey began with Java (which has not been long). But I do know that anonymous classes are a God-send, which are considered inner classes. I've used them before, to aid in my studying for the SCJP2 test, but when I write programs, even long ones, I've yet to find a productive use for them except for the anonymous classes. I've never coded in the "real world" though, so take my opinion with a grain of salt.
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.

Michael J Bruesch<br /><i>I code, therefore I am.</i>
I agree. Here's the link:
subject: Inner Classes
It's not a secret anymore!