7 Replies Latest reply on Jun 14, 2007 10:26 AM by thomas_behr

    Inner class doubt

    807605
      class OuterTest {
           String id ;
           OuterTest ( ) {
                this . id = " Default " ;}
           OuterTest ( String id ) {
                this . id = id ;     }
           class InnerTest extends OuterTest {
      
                void doSomething ( ) {
                     System . out . println ( OuterTest . this . id ) ;          }
           };
           public static void main ( String args [ ] ) {
                OuterTest outer = new OuterTest ( " STP " ) ;
                InnerTest inner = outer . new InnerTest ( ) ;
                inner . doSomething ( ) ;     }
      };
      As per my understanding, it should print Default. Because, the InnerTest constructor calls internally the no-argument constructor of the OuterTest, which sets the id = Default. But, its print STP.

      Please explain.
        • 1. Re: Inner class doubt
          807605
          plz help
          • 2. Re: Inner class doubt
            807605
            As per my understanding, it should print Default.
            plz explain
            Because, the InnerTest constructor calls internally
            the no-argument constructor of the OuterTest,
            where does this understanding come from? what makes you think so?
            which
            sets the id = Default. But, its print STP.
            please list the possible explanations...
            • 3. Re: Inner class doubt
              807605
              InnerClass is a sub class of OuterTest. Hence, the default constructor of the InnerClass calls the default constructor of the OuterTest, which set the id = Default.
              • 4. Re: Inner class doubt
                thomas_behr
                InnerClass is a sub class of OuterTest. Hence, the
                default constructor of the InnerClass calls the
                default constructor of the OuterTest, which set the
                id = Default.
                But that doesn't matter since you are not reporting the ID of InnerTest but the one form its enclosing OuterTest, which is not reassigned when a new instance of InnerTest is created. outer and inner do not share the same instance of the ID String!
                class OuterTest {
                    String id ;
                    OuterTest ( ) {
                        this . id = " Default " ;
                    }
                
                    OuterTest ( String id ) {
                        this . id = id ;
                    }
                
                    class InnerTest extends OuterTest {
                 
                        void doSomething ( ) {
                            System . out . println ( OuterTest . this . id );
                        }
                
                        void doSomethingElse() {
                            System.out.println(InnerTest.this.id);
                        }
                    }
                
                    public static void main ( String args [ ] ) {
                        OuterTest outer = new OuterTest ( " STP " ) ;
                        InnerTest inner = outer . new InnerTest ( ) ;
                        inner . doSomething ( ) ;
                        inner.doSomethingElse();
                    }
                }
                • 5. Re: Inner class doubt
                  807605
                  okay... let's give this a go

                  how many OuterTest objects are there?

                  there's the one that you instantiate with "STP", and the one that is created as base class as you instantiate InnerTest. so, looking at it from your (one) InnerTest instance, there's an enclosing OuterTest (the first one) and a base OuterTest (the second one).

                  so how many OuterTest.id's are there?
                  which of these does "OuterTest . this . id" refer to?
                  what happens if inside doSomething you also print out "super.id"?

                  btw, your formatting style is horrible, really horrible
                  • 6. Re: Inner class doubt
                    807605
                    formatting...you mean, the way the code is presented?
                    It's not me...i got this from an exam questionnaire
                    • 7. Re: Inner class doubt
                      thomas_behr
                      formatting...you mean, the way the code is
                      presented?
                      It's not me...
                      Right, it wasn't you who posted that code. And since it wasn't you, who posted that code, you could not have formatted that code any other way.

                      i got this from an exam questionnaire
                      And are you able to answer the questionaire now with the hints/explanations given to you?