This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner Classes" Watch "Inner Classes" New topic
Author

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?
Martin
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

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"
Sheriff

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
Jason
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.
Cheers,
Martin
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.
http://www.geocities.com/mjbruesch


Michael J Bruesch<br /><i>I code, therefore I am.</i>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Classes
 
Similar Threads
What is a class file with $ symbol in the name
Doing some "cleanup" before closing a program, how?
brackets in the middle of a method call?
When to use inner class?
inner classes