Link to home
Start Free TrialLog in
Avatar of hey_hey
hey_hey

asked on

what is the Exact need of Inner Class?

hi,
I want to knwo why the Inner class are implemented in Java.

wath the exact reason aside from being there as Listener classes?
Avatar of hey_hey
hey_hey

ASKER

it would be good if somebody gabve a link to some site
Inner classes come under 4 categories:

1. A class declared inside another class
   Class A{
      Class B{}
   }
   In this case Class B follows instantiation rules rather than package rules. It has complete access to Class A's private/protected/public resources

2. Top level nested class
    Class A{
       static Class B{}
    }
    IN this case only static vraibles/objects/methods of class A is accessible inside class B 'cos non-static values canoot be accessed inside a static block

3. Class declated inside a method
   class A{
       public void method1{
            class B{}
       }
   }
   In this case u use it 'cos u just want to use & throw the class B. non-final variables/objects of method1 r not accessible inside class B 'cos it wud cause inconsistency.

4. Last comes the anonymous class
    u commonly use this for listeners. This is a class embedded inside an expression
     a.addXXXlistener(new XXX(){}));
   there is no handle for this class & if u need it again u have to declare it again.

-sgoms
Apart from being a handy way of organising code sometimes a generally accepted reason for having inner classes is to create control frameworks. An explanation of this follows....

"Why did Sun go to so much trouble to add such a fundamental language feature as inner classes in Java 1.1? The answer is something that I will refer to here as a control framework .

An application framework is a class or a set of classes that is designed to solve a particular type of problem.
To apply an application framework, you inherit from one or more classes and override some of the methods. The code you write in the overridden methods customizes the general solution provided by that application framework to solve your specific problem. The control framework is a particular type of application framework dominated by the need to respond to events; a system that primarily responds to events is called an event-driven system. One of the most important problems in application programming is the graphical user interface (GUI), which is almost entirely event-driven. "
If you really want to know how the compiler makes inner classes, look at this example:

Compile the following

class Animal {
  class Bird {
  }
}

and you find that you get two classes - one called Animal.class and another called Animal$Bird.class. The compiler is just manipulating class names to swizzle you into thinking that there really are inner classes as a separate phenomenon to ordinary classes.

If you look at these using the javap utility, you can then see how the compiler transforms inner classes into something it understands. This is why you must NEVER use a $ sign in your class names, as things will start to get real weird...
Sorry about that, but the system refused to let was refusing to let me post large comments just now...which has happened before.

I'll try and post it as one large comment again when it is working again...
Apart from being a handy way of organising code sometimes a generally accepted reason for having inner classes is to create control frameworks. An explanation of this follows....

"Why did Sun go to so much trouble to add such a fundamental language feature as inner classes in Java 1.1? The answer is something that I will refer to here as a control framework .

An application framework is a class or a set of classes that is designed to solve a particular type of problem. To apply an application framework, you inherit from one or more classes and override some of the methods. The code you write in the overridden methods customizes the general solution provided by that application framework to solve your specific problem. The control framework is a particular type of application framework dominated by the need to respond to events; a system that primarily responds to events is called an event-driven system. One of the most important problems in application programming is the graphical user interface (GUI), which is almost entirely event-driven. "

http://codeguru.developer.com/java/tij/tij0081.shtml


If you really want to know how the compiler makes inner classes, look at this example:

Compile the following

class Animal {
  class Bird {
  }
}

and you find that you get two classes - one called Animal.class and another called Animal$Bird.class. The compiler is just manipulating class names to swizzle you into thinking that there really are inner classes as a separate phenomenon to ordinary classes.

If you look at these using the javap utility, you can then see how the compiler transforms inner classes into something it understands. This is why you must NEVER use a $ sign in your class names, as things will start to get real weird...
that's better...
btw. Jod, have you posted information regarding 'problems while posting large comments' to the Customer Service forum ?
I once sent an email to the email address given in the error page to report it but didn't get much response. The Customer service forum is usually more responsive, but I find the problem to be so intermitent that it is hard to reproduce easily.

You know me though - I'm not one for long answers...never more than ten words.
;-)
Avatar of hey_hey

ASKER

hello all,

I wanted to know the reason that the inner classes are there in Java,but some have said abt featuers

from
http://java.sun.com/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc3.html

can somebody explain their nedd?

thanks all
With Inner classes u can implement Multiple inheritance in Java
Pardon me if I am wrong but I can continue on tommorrow due to time constraints.
Using an Inner Class to Implement an Adapter:
http://java.sun.com/docs/books/tutorial/java/javaOO/innerClassAdapter.html

what r inner classes good for:
http://java.sun.com/docs/books/tutorial/java/more/innerclasses.html

Reduce class clutter in your Java designs: Use inner classes:
http://www.javaworld.com/javaworld/jw-10-1997/jw-10-indepth_p.html

-sgoms

What a fine discussion?????

 
ASKER CERTIFIED SOLUTION
Avatar of mbormann
mbormann

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hey_hey

ASKER

Ok thanks all who shall I give points to?

heyhey thansk for a good URL,sgoms too.
Jod andmbormmann .

please say!
(1)u can dlete this ques and post qeustion(s) for those who u feel helped u.

(2)ask somebody to lokc it and post separate ques for somebody else.
'accept comment as answer' for one person here & post empty questions for others
sgoms ,
I forgot that completely! I havent got used to it yet.
Avatar of hey_hey

ASKER

mbormann i am not intesrested in mutiple inheritance but exaplain so get points,
posting new questoins for jod,sgoms ,heyhey