• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread - Please explain!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the output for the following code as "vandeleur".Can anybody explain please?Thanks.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Why shouldn't it be vandeluerwiggy 0 1 ..like that>can u please explain.Thanks
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bijesh Krishnadas
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic