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?
hey_heyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hey_heyAuthor Commented:
it would be good if somebody gabve a link to some site
0
heyhey_Commented:
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

sgomsCommented:
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
0
JodCommented:
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....

0
JodCommented:
"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 .

0
JodCommented:
An application framework is a class or a set of classes that is designed to solve a particular type of problem.
0
JodCommented:
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. "
0
JodCommented:
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...
0
JodCommented:
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...
0
JodCommented:
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...
0
JodCommented:
that's better...
0
heyhey_Commented:
btw. Jod, have you posted information regarding 'problems while posting large comments' to the Customer Service forum ?
0
JodCommented:
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.
0
sgomsCommented:
;-)
0
hey_heyAuthor Commented:
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
0
mbormannCommented:
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.
0
sgomsCommented:
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

0
Ravindra76Commented:
What a fine discussion?????

 
0
mbormannCommented:
Jod is in the right direction and ur question should have been like what r Anonymous classes r for.I will continue what he meant ...

as sgoms pointed out for Anonymous classes
'there is no handle for this class & if u need it again u have to declare it again'

In VC++ (MFC & C SDK) as you may know teh program you write sends messages to Windows and Windows sends mesages to you,
Now Windows sends all kinds of messages,
In C SDK you have 1 big switch inside which u have case statements of Events u want to trap and take action on ,say LBUTTONDOWN whcih means trap Mouse Left button (I am not sure of syntax).This is an inelegant way to code an Event handler.

Sometimes what happens the message si sent to a Doc class which has no handler written for it and goes to a View Class and so on it propogates from one thing to other until it is handled somewhere,if it is not then Windows handles it by default in DefWindowProc() ,this last is to take no action at all.

but in Java Anonymous classes you have the perfect Event handler,the event generated by this class whihc has registered for that is caught in that class only and rest all events are ignored.

Also I know that I am digressing from this dicsussion but ...
As can be seen elsewhere in the PAQ at
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10237885 
we can implement a crude form of Multiple inheritance uisng Inner class,which cuts down on 1 class which would have to be passed in constructor.


class Dummy extends Base1
{
      public String dummy;
      
      public Dummy()
      {
            super();
            dummy="dummy";
      }

      class ActualClass extends Base2
      {
            public String actual;
            public Dummy ref;
            
            public ActualClass(Dummy ref)
            {
                  super();
                  actual="actual";
                  this.ref=ref;
            }
      }
      
      public static void main(String[] args)
    {
            Dummy b = new Dummy();
            Dummy.ActualClass a = b.new ActualClass(b);
            System.out.println("Testing Multiple Inheritance in Java using Inner Classes & Composition,a crude way of doing it.");

            Class superOfA = a.getClass().getSuperclass();
            System.out.println("SuperClass of ActualClass="+superOfA.toString());
            
            Class superOfB = a.ref.getClass().getSuperclass();
            System.out.println("SuperClass of Dummy="+superOfB.toString());
    }
}

class Base1
{
      public String base1;
      public Base1()
      {
            base1="base1";
      }
      
      public String returnBase1Str()
      {
            return base1;
      }
}

class Base2
{
      public String base2;
      public Base2()
      {
            base2="base2";
      }

      public String returnBase2Str()
      {
            return base2;
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hey_heyAuthor Commented:
Ok thanks all who shall I give points to?

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

please say!
0
mbormannCommented:
(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.
0
sgomsCommented:
'accept comment as answer' for one person here & post empty questions for others
0
mbormannCommented:
sgoms ,
I forgot that completely! I havent got used to it yet.
0
hey_heyAuthor Commented:
mbormann i am not intesrested in mutiple inheritance but exaplain so get points,
posting new questoins for jod,sgoms ,heyhey
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.