Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Objects- references

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test7{
public Test7(){}
public Test7(Test7 ref){
this (ref,"Hai");
}
public Test7(Test7 ref,String str){
ref.Test7(str);
System.out.println("Hi");
}
public void Test7(String str){
System.out.println(str);
}
public static void main(String[] args){
Test7 t = new Test7();
Test7 t7 = new Test7(t);
}
}

can anyone explain to me how do we get the output
hai
hi.
I just don't understand
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructor invoked with new Test7(t) has a first line of this(ref, "Hai"). Can you describe what this line does?
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how I trace it (note it's a little clumsy looking but hopefully will help):

which calls:

I think that's right...I'm getting confused using the 'CODE' formatting and all, but the output made sense to me tracing it that way. Note that the call to:

public void Test7(String str)


in the line:

is probably where you got confused. This called the method which did the first print out AND THEN the flow moved to the next line. I hope this helps...wish I could say it better, but that's what's going on in my mind at least (I haven't tested the code myself) I'd also suggest sticking a ton of console output through there if you're still confused so you can really see the flow of execution which is where I think you're having trouble. Just go ahead and print every damn thing out like: System.out.println("In MyClass::foo, before call to bar()..."). Eventually, you need to be able to trace it without doing that, but it's the first step in understanding the flow. Hope that doesn't sound too pedantic and all, just a suggestion

[ November 20, 2007: Message edited by: nico dotti ]
[ November 20, 2007: Message edited by: nico dotti ]
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic