File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Can we create an inner Class for an INNER CLASS?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we create an inner Class for an INNER CLASS??" Watch "Can we create an inner Class for an INNER CLASS??" New topic

Can we create an inner Class for an INNER CLASS??

Justin Smith

Joined: Jul 24, 2008
Posts: 19
Hi I have one doubt regarding Inner class... This is different than the other topic though... Can I create an Inner Class for a INNER class?? what will happen if I create and what is the use of it???

I tried creating it and was able to create one, but I wish to know is there any real world scenario which would need this kind of coding....

The sample example is given below:

import java.util.*;
import java.lang.*;

public class MyOuter implements
private int x = 6;
private String name = "Humpty Dumpty";
public MyOuter(){System.out.println("Calling the Outermost Constructor");}
public MyOuter(String arg){}
public double seeCurrent()
float f = 3.1416f;
Double d = Double.valueOf(f*x);
return d;
class MyInner
public MyInner(){System.out.println("Calling the MyInner Constructor");}
public void seeOuter()
System.out.println("The value of the 'x' is :"+x);
class MyInnerMost
public MyInnerMost(){System.out.println("Calling the MyInnerMost Constructor");}
public void seeOuterMost()
System.out.println("The 'name' selected for $1 million dollar is:"+name);

public static void main(String[] are) throws java.lang.Throwable
System.out.println("Before creating any instance in the main method");
Double value = null;
MyOuter mo = new MyOuter();
value = mo.seeCurrent();
System.out.println("The value of the return value is :"+value);
MyOuter.MyInner mi = new MyOuter().new MyInner();
MyOuter.MyInner.MyInnerMost mim = new MyOuter().new MyInner().new MyInnerMost();
Garrett OBrien

Joined: Oct 25, 2005
Posts: 8
Yes, that is possible (as you found out), and you would use an inner-inner class for the same reasons you would use just an inner class, however, I don't think I would ever do this because it looks fairly ugly, and anyone coming back to it would probably be confused.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I have created anonymous inner classes inside anonymous inner classes quite regularly. It's a natural thing: "Okay, I have this JButton, and when it's clicked it executes an action listener in an anonymous inner class. Now, what this action listener does is to create an UndoableEdit, which has to do something specific to the situation so it's also an anonymous inner class."

Notice that the second one is inside the first one. Not hard to understand, really. But I can't see any use for a named inner class inside another named inner class.
I agree. Here's the link:
subject: Can we create an inner Class for an INNER CLASS??
It's not a secret anymore!