• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Classes

 
Martin Rennix
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get by without knowing them in detail. (They are just a simple way of hiding a class inside of another class.)
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic