Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread - Please explain! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread - Please explain!" Watch "Thread - Please explain!" New topic
Author

Thread - Please explain!

Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
I get the output for the following code as "vandeleur".Can anybody explain please?Thanks.


Software_guy
fethi makhlouf
Ranch Hand

Joined: Feb 16, 2004
Posts: 50
Hi Sridhar
that's ur code in a more "readable" way ....
public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
}
}

i compiled it and it run the result is vandeleur 0 1 2 3
wher is problem?


SCJP 1.4
Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117
Hi!
Why shouldn't it be vandeluerwiggy 0 1 ..like that>can u please explain.Thanks
Giray �nver
Greenhorn

Joined: Feb 23, 2004
Posts: 1
Hi Sridhar,
Because the "piggy(String sName)" method uses just a copy of the string object refrenced by instance variable "static String sName". So any modifications you make on it (in piggy method), does not affect the actual object.
You may consider giving a return type to the piggy method and changing the code in your main method accordingly.
Rgds.
Kadiyala Naga Sai
Greenhorn

Joined: Feb 11, 2004
Posts: 20
Hi Sridhar,
Try this code , u may understand...If still can't , post reply.I will explain.This is very long to explain.But I will patiently do that.
public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println("Main method print>>"+sName);
}
public String piggy(String sName){
Tux.sName = Tux.sName + " wiggy";
System.out.println("Piggy method print>>"+Tux.sName);
start();
return Tux.sName;
}
public void run(){
System.out.println("Print1 from run method>>"+sName);
for(int i=0;i < 4; i++){
sName=sName + " " + i;
}
Tux.sName=sName;
System.out.println("Print2 from run method>>"+sName);
}
}


Kadiyala S Naga Sai<br />Petronas Ammonia Sdn Bhd<br />Kerteh,Malaysia 24300
Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
I just got confused. I thought changes made to an object passed to a method are reflected outside the method, i.e. the method actually receives a copy of the reference (not object).
Here sName is an object, then why doesn't the change get reflected.
Consider code below

When the change to Test object in method1 is seen outside the method, why doesn't the same thing happen with the String object. I know I am missing something very simple here but cannot figure it out.
TIA,
Bijesh
Shahfazal Mohammed
Ranch Hand

Joined: Jan 08, 2004
Posts: 41
I believe what's happening there is you are not updating String s1 with the return string of ur method2(). that way the string "This is wierd" is lost. now if you'd said

s1 would have been changed to "this is wierd". i'm not able to look at ur code while im posting this answer right now, guess i got my variables wrong, but hope you got the idea


-peace.
Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
Okie I got it figured out...
From Java Tutorial on Sun:

In Java methods, arguments are passed by value. When invoked, the method receives the value of the variable passed in. When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object.

So method2() was trying to change the reference of the String object and this change will not be available outside of the method... Pretty simple, huh?
fethi makhlouf
Ranch Hand

Joined: Feb 16, 2004
Posts: 50
Hello
i think u can get it more simplly....
public class Tux extends Thread{
static String sName = "vandeleur"; // class member Called sName
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName){ // argument is called also sName, it will put static member in shadow!
sName = sName + " wiggy"; // All changement done to sName here will affect that variable but not the class one!
start();
}
public void run(){
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
}
}

Hope it's clear for you!
Bob Chandler
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
public class Tux extends Thread{
static String sName = "vandeleur"; // class member Called sName
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName); // if this line was changed to.. t.sName = t.piggy(sName);
// and piggy had the statement.... return sName;
// would the value of the class variable sName be altered?


System.out.println(sName);
}
public void piggy(String sName){ // argument is called also sName, it will put static member in shadow!
sName = sName + " wiggy"; // All changement done to sName here will affect that variable but not the class one!
start();
}
public void run(){
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread - Please explain!
 
Similar Threads
urgent!!!! Marcus Test 1 Threads
Confusion about a question
start()
Thread!
Passing Values