• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Inner Classes  RSS feed

 
Ranch Hand
Posts: 34
  • 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
 
Ranch Hand
Posts: 1066
2
IntelliJ IDE Java Spring
  • 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.)
 
"The Hood"
Sheriff
Posts: 8521
  • 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).]
 
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 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
  • 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
 
Ranch Hand
Posts: 158
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!