Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we create a Inner Class for a 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???
 
Baseet Ahmed
Ranch Hand
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,you can.



Regards
Baseet Ahmed
 
Justin Smith
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sumeet,

Thanks for your reply !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic