• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

when we need inner classes

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody tell when we actually need to use inner classes in real world programming ? Any body who has used inner classes for writing any commercial Java application ?
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mostly for event handling. anonymous inner classes
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
inner classes helps you hide your code from others outside your class.
 
Shrini Kulkarni
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not clear to me. To do event handling !! can any body give an example. why it can be done using a method why we need a class ?
and as far hinding code ... can you some body explain further ??
To me it looks that inner classes are just there - "a nice to have" kind of feature. we can live in java without them in almost all conditions
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Inner class can make the code compact when performing event handling
for example :-
if you are using an applet with mouse events either you need to implement an interface or an adapter. Interfaces should be defined with all the event methods atleast with dummy functions,if adapters are used you cannot extend both applet and MouseAdapter, though we can define a separate class for the MouseAdapter, still we need to give additional code to access Applet's data and methods inside the Mouse Event Class..
in this scenario if InnerClass is used the task and the code will be very simple
// inside init method of Applet
// anon inner class
addMouseListener(new MouseAdapter())
{
/*here your mouse code is given...
the code given here can access applet's data and methods because inner class can access all the private data.. of the enclosing class
*/
});
Inner Class can totally encapsulate all inner class code which is solely needed only by the class where it is defined...
with inner class you can encapsulate the class declaration itself... while private encapsulates data members and methods..
if the inner class code can be re-used in other part of your application you won't normally go for Inner Class even though you can do so my making public inner classes

Note:- you can almost do everything without using inner class

[This message has been edited by geek oak (edited February 04, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic