Nested and Inner Classes

What is the need of nested and inner classes in Java? Explain where they are useful?

Questions by venkatSeeni

Showing Answers 1 - 12 of 12 Answers

shabrena

  • Mar 24th, 2009
 

In nested class the variable and methods from the outer class can be invoked by the inner class. Whereas the variable and of inner class cannot be invoked by outer class.

  Was this answer useful?  Yes

msuresh70

  • May 23rd, 2009
 

Nested class is static class wrapped inside the outer class
Inner class is non-static class wrapped inside the outer class which has access to the instance variables of outer class.

  Was this answer useful?  Yes

Sarje

  • Aug 20th, 2009
 

Using Inner classes you can write more efficient code and even reduce the typing work. Inner classes are frequently used in Event Handling. For example to MouseEvent event is handled in following example:

import java.applet.*;
import java.awt.event.*;
public class AnonymousInnerClassDemo extends Applet
{
         public void init(){
           addMouseListener( new MouseAdapter(){
                    public void mousePressed(MouseEvent me) {
                        showStatus("mouse pressed");
        }
         });
   }
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions