This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes wrong output? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "wrong output?" Watch "wrong output?" New topic
Author

wrong output?

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
public class Test7{
public Test7(){}
public Test7(Test7 ref){
this (ref,"Hai");
}
public Test7(Test7 ref,String str){ /***what i thought as a problem***/
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);
}
}



/**************question***********/
the output i get is Hai Hi
but according to what i understand it should be a compiler error because there is a constructer which takes two arguments. but there are none passed from main*********/ /**** can anyone help with "this" keyword iknow it refers to the current object**** i need a bit more explanation with example*** like when it is passed as a parameter what is happening******/
Maurizio Nagni
Ranch Hand

Joined: May 29, 2004
Posts: 75
you can have two "kinds" of this

The first is the typical this. This is the reference to the instance.
for example in this case you assign a value (test) to an parameter (this.test) of the instance of the class.

The second is this(par1, par2, ....). In this case you are NOT referring to an class instance because the class is still not instantiated but you are requiring a specific constructor of that class.

and it is in the second meaning that you are using the this when you write ; when you have realize this fact you can follow the constructors and understand the reason of your output: you are instantiating two object but only the second (t7) is the reason of the output.

Ciao
 
 
subject: wrong output?