Method reffering to an object
HY
There is a problem , i have a method that takes as an argument a Thread object; and by all specs it should take it by refference not by value; but my method seems to do backwards; this is the method :
public void startThread (Thread e) {
if(e == null) {
e = new Thread(this);
System.out.print(e+"\t\t"+t+"\n\n\n");
};
}
and for who wants the whole file here it is :
import javax.swing.*;
class updateThread
implements Runnable {
Thread t,w;
int s;
boolean b;
public updateThread(){};
public static void main (String args[]) {
updateThread obj = new updateThread();
obj.startThread(obj.t);
}
public void startThread (Thread e) {
if(e == null) {
e = new Thread(this);
System.out.print(e+"\t\t"+t+"\n\n\n");
};
}
public void run() {
try {
while(true ) { s++;
System.out.print("\n\n\n\n\t for s= "+s+"\n\t thread t "+ t+"\n\n\t thread w "+w);
Thread.sleep(1000);
if(s == 3) {stopThread();s=0;};
}
}
catch(Exception e) { System.out.print("\n\n\n\t Exception "+e+"\n\n\n\n");};
}
public void stopThread ( ) {
if(true) {
b = !b;
if(!b) { w = t;
t= null;}else{t = w; w = null;};
System.out.print("\n\n\n\n\t\t ****** thread change ********\n\n\n");
}
}
}
Ok ,so if u just watch closely at my method u see (after compilation)that e(pointing to obj.t) takes a value (from code : e = new Thread(this);) so ok but it does not affect t aswell; why? it should as it is passed by refference .
Thank u in advance.
|