Intricate Inner Class Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xemnosyst
    New Member
    • Oct 2006
    • 4

    #1

    Intricate Inner Class Question

    Hello. I have an obscure question for y'all. Consider an inner class:

    Code:
    class Outer1 {
        class Inner1 {
        }
    }
    Now in a different inner class I extend the first one. Does anyone know how to test if <i is an inner class of o> in "aMethod", below?

    Code:
    class Outer2 {
        Inner1 i;
        
        class Inner2 extends Outer1.Inner1{
            Inner2(Outer1 o) {
                o.super();
            }
        }
    
        public void aMethod(Outer1 o) {
            if ([I][B]<i is an inner class of o>[/B][/I]) {
                ....
            }
        }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Xemnosyst
    Hello. I have an obscure question for y'all. Consider an inner class:

    Code:
    class Outer1 {
        class Inner1 {
        }
    }
    Now in a different inner class I extend the first one. Does anyone know how to test if <i is an inner class of o> in "aMethod", below?

    Code:
    class Outer2 {
        Inner1 i;
        
        class Inner2 extends Outer1.Inner1{
            Inner2(Outer1 o) {
                o.super();
            }
        }
    
        public void aMethod(Outer1 o) {
            if ([I][B]<i is an inner class of o>[/B][/I]) {
                ....
            }
        }
    }
    Your code is not correct. Inner1 i; will give a compilation error. You can only declare objects of type Inner1 by getting in the context of Inner1 first which is Outer1. As in Outer1.Inner1 i;
    Now your question does not make much sense since inner classes are members of classes not of objects. A class is an inner class of another class not of an object. If you change your question to how to test if a class is an inner of a given class, then such a question would be redundant since the syntax would require something like
    if Outer.Inner isInnerOf Outer and you can see that it is both redundant and impractical to have such a comparison.

    Comment

    • Xemnosyst
      New Member
      • Oct 2006
      • 4

      #3
      Here is a full working example which I just compiled:

      Code:
      public class Outter2 {
      	public static void main(String[] args) {
      		Outter1 o1 = new Outter1();
      		Outter2 o2 = new Outter2(o1);
      		o2.aMethod(new Outter1());
      	}
      
      	Inner2 i2;
      
      	Outter2(Outter1 innerParent) {
      		i2 = new Inner2(innerParent);
      	}
      
      	void aMethod(Outter1 o) {
      		System.out.println("is o the parent object of i2?");
      	}
      
      	class Inner2 extends Outter1.Inner1 {
      		Inner2(Outter1 parent) {
      			parent.super();
      			System.out.println("success!");
      		}
      	}
      }
      
      class Outter1 {
      	class Inner1 {
      		Inner1() {
      			System.out.println("success?");
      		}
      	}
      }
      The output is:
      success?
      success!
      is o the parent object of i2?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Xemnosyst
        Here is a full working example which I just compiled:

        Code:
        public class Outter2 {
        	public static void main(String[] args) {
        		Outter1 o1 = new Outter1();
        		Outter2 o2 = new Outter2(o1);
        		o2.aMethod(new Outter1());
        	}
        
        	Inner2 i2;
        
        	Outter2(Outter1 innerParent) {
        		i2 = new Inner2(innerParent);
        	}
        
        	void aMethod(Outter1 o) {
        		System.out.println("is o the parent object of i2?");
        	}
        
        	class Inner2 extends Outter1.Inner1 {
        		Inner2(Outter1 parent) {
        			parent.super();
        			System.out.println("success!");
        		}
        	}
        }
        
        class Outter1 {
        	class Inner1 {
        		Inner1() {
        			System.out.println("success?");
        		}
        	}
        }
        The output is:
        success?
        success!
        is o the parent object of i2?
        What is this supposed to prove? The code now compiles because Inner2 i2; is done in the correct context (inside the class where it has been defined) and the reference to Inner1 inside Outter2 has been done through Outter1 which is where it has been declared.

        Comment

        • Xemnosyst
          New Member
          • Oct 2006
          • 4

          #5
          Originally posted by r035198x
          What is this supposed to prove? The code now compiles because Inner2 i2; is done in the correct context (inside the class where it has been defined) and the reference to Inner1 inside Outter2 has been done through Outter1 which is where it has been declared.
          Oh!! I see now. Yes, you are right, my original code was incorrect. That member variable should have been "Inner2 i2", like in the version that compiles. Sorry about that. But the questions remains: how can I test whether "o" is the outter object for "i2" in "aMethod"?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Xemnosyst
            Oh!! I see now. Yes, you are right, my original code was incorrect. That member variable should have been "Inner2 i2", like in the version that compiles. Sorry about that. But the questions remains: how can I test whether "o" is the outter object for "i2" in "aMethod"?
            Like I said this not neccessary to test and there probably is not a way to do it without messing with lots of reflection. Inner classes only allow grouping of classes together and splitting of a class into smaller more manageable classes. Trying to test for being an inner class is like trying to test if two classes are in the same package

            Comment

            • Xemnosyst
              New Member
              • Oct 2006
              • 4

              #7
              Originally posted by r035198x
              Like I said this not neccessary to test and there probably is not a way to do it without messing with lots of reflection. Inner classes only allow grouping of classes together and splitting of a class into smaller more manageable classes. Trying to test for being an inner class is like trying to test if two classes are in the same package
              Is anyone sure whether I have to use reflection to accomplish this test? I am in a situation to need that information in a project I'm working on.

              Comment

              Working...