Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Explanation needed for a question

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Can someone explain why I get "STP as output from this program even though the println statements show that my understanding that "default" is the last value assigned is correct, in the program below:



public class OuterTest {

String id ;

OuterTest ( ) {
this . id = " Default " ;
System.out.println ("No-args constructor called");
System.out.println ("Outertest.this.id : " + this.id);
}

OuterTest ( String id ) {
this . id = id ;
System.out.println ("Single-args constructor called");
System.out.println ("Outertest.this.id : " + this.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 ( ) ;
}
};




The output I got was



Single-args constructor called
Outertest.this.id : STP
No-args constructor called
Outertest.this.id : Default
STP



How come then when [B} doSomething [/B} is callef, the value is "STP"

Regards,
Padmanabhan
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try to explae you.



Line 4 will call function on Line 2, and the Line 3 will be excuted as follows
OuterTest . this . id whcih can be looked as
OuterTest-->CurrentlyExceutingThread-->its id
So which makes it as
OuterTest.Outer.id which is STP


To get the default as output in the last line, you have to change the Line 1 to
static String id ;

Ranchers
Correct me if i'm wrong.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
void doSomething ( ) {
System . out . println ( id ) ;
}


Note that I have changed the method to print just id. Now you will get your desired output of Default. This has to do with inner class inheritance.


In your previous code, you were explicitly printing the OuterClass.this.id and hence it was printing the STP.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

In Line 1 we are creating an object of OuterTest with one argument. Thus id for that object will be set as �STP�.

Now in Line2 we use the above created object to create an object of InnerTest class.
So for InnerTest�s object, id will hold the value �Default�.



Now in doSomething method we are calling OuterTest.this.id which means we want the value of id for the Parent class of the current object. Ie id of the object through which the object of InnerTest was created. Thus this means that we are trying to fetch the value of id for the object called �outer�. Thus it prints �STP�.

If we want to print �Default� than just modify doSomthing() method as follows



Now the last line of o/p will be �Default�. This is b�cos value of id is set as �Default� by the constructor of InnerTest which calls its parents� no argument constructor,
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic