Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 20th, 2007, 09:30 AM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default a Thread argument

HY

I am kind of new around her but i studied java since 6 months;

so this is the problem , i have a method that takes as an argument a Thread object; an 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.

  #2 (permalink)  
Old February 21st, 2007, 06:12 AM
Authorized User
 
Join Date: Feb 2007
Location: Prague, , Czech Republic.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

e = new Thread(this);

means e is now a reference to a new Thread object. what was e referencing before doesn't matter(null or another Thread object).

I think you missed that references are passed by value, i.e.
Code:
t = new Thread();
e = t;
... changes to t and e are equal
e = new Thread();
... t and e now reference different objects


Similar Threads
Thread Thread Starter Forum Replies Last Post
Argument not Optional aziaraphale Excel VBA 5 August 1st, 2007 02:53 AM
Invalid Argument ppenn Access VBA 2 May 14th, 2007 10:11 AM
rows(argument) dabith Excel VBA 2 June 13th, 2004 06:13 AM
Invalid Argument.... baNTON Access VBA 4 April 27th, 2004 06:18 AM
Tablename argument chloe Access VBA 7 March 24th, 2004 12:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.