This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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

Justin Smith
Greenhorn

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 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

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.

Garrett
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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: http://aspose.com/file-tools
 
subject: Can we create an inner Class for an INNER CLASS??
 
Similar Threads
accessing shadowed final values in method local inner class
Inner class..
about inner classes
Inner Class question..
Can we create a Inner Class for a INNER Class?