wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can we create a Inner Class for a 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can we create a Inner Class for a INNER Class?" Watch "Can we create a Inner Class for a INNER Class?" New topic
Author

Can we create a Inner Class for a 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???
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Yes,you can.



Regards
Baseet Ahmed
Justin Smith
Greenhorn

Joined: Jul 24, 2008
Posts: 19
Hi Baseet,

I tried creating one and was able to create it, but i wish to know is there any real world scenario which would need a scenario like this???

Here is my sample program....


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 extends java.lang.Thread
{
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)
{
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();
}
}

The output is :
---------------

The value of the return value is :18
Calling the Outermost Constructor
Calling the MyInner Constructor
The value of the 'x' is :6
Calling the Outermost Constructor
Calling the MyInner Constructor
Calling the MyInnerMost Constructor
The 'name' selected for $1 million d
Press any key to continue . . .
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

Sure! you can do so.But as you observe, this programming practice is a sort of obfuscating the logic.Even you can do various other combinations with method-local/argument specific local classes(and their sub-classes).

In actual programming development if you feel the need, you should reconsider your design/programming practice(well I am not sure here).Mainly we need inner classes to handle event driven logic in GUI but other than that, OOP in my opinion eliminates this need for code cluttering/repetitive logic.
Justin Smith
Greenhorn

Joined: Jul 24, 2008
Posts: 19
Hi Sumeet,

Thanks for your reply !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we create a Inner Class for a INNER Class?
 
Similar Threads
Reason to create inner class in an interface
Whats the use of inner class?
Problem with Class.forName()
Question regarding Innerclass....
inner class