Inner class doubt

class OuterTest{

String id ;

OuterTest ( ){

this . id =" Default " ;}

OuterTest ( String id ){

this . id = id ;}

class InnerTestextends OuterTest{

void doSomething ( ){

System . out . println ( OuterTest .this . id ) ;}

};

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

[1473 byte] By [ramkria] at [2007-11-27 7:38:57]
# 1
plz help
ramkria at 2007-7-12 19:19:32 > top of Java-index,Java Essentials,Java Programming...
# 2

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

OnBringera at 2007-7-12 19:19:32 > top of Java-index,Java Essentials,Java Programming...
# 3
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.
ramkria at 2007-7-12 19:19:32 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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();

}

}

thomas.behra at 2007-7-12 19:19:32 > top of Java-index,Java Essentials,Java Programming...
# 5

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

OnBringera at 2007-7-12 19:19:32 > top of Java-index,Java Essentials,Java Programming...
# 6
formatting...you mean, the way the code is presented?It's not me...i got this from an exam questionnaire
ramkria at 2007-7-12 19:19:32 > top of Java-index,Java Essentials,Java Programming...
# 7

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

thomas.behra at 2007-7-12 19:19:32 > top of Java-index,Java Essentials,Java Programming...