• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Justin Smith
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 java.io.Serializable
{
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();
mi.seeOuter();
MyOuter.MyInner.MyInnerMost mim = new MyOuter().new MyInner().new MyInnerMost();
mim.seeOuterMost();
}
}
 
Garrett OBrien
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Garrett
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic