Results 1 to 10 of 10
  1. #1
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default how I transfer/use vars or/and methods from a Thread subclass -to-> the class of main

    how I transfer/use vars or/and methods from a Thread subclass -to-> the class of main() and vice versa ?

    a subclass like":
    class MyThread extends Thread
    {
    may contain getters / setters run from main() to get values from subclass ?

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    When you create an instance of your Thread in the main() the main() will have access to the public methods of your thread object.

    Java Code:
    public static void main(String[] args)
    {
       MyThread myThread = new MyThread();
    
       myThread.setSomeValue("blah");
       System.out.println( myThread.getSomeValue() );
       
       myThread().start();
    }
    Last edited by mrmatt1111; 09-19-2009 at 06:51 PM.
    My Hobby Project: LegacyClone

  3. #3
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default If I have:

    If I have:
    public String getAd() { return ad; }
    in a getter and
    ad var
    setup via
    String ad = input.nextLine();
    in run()
    running in main()(other class) the
    x=advt.getAd();
    it will work ? where is error ? when call the
    x=advt.getAd();
    prior or after advt.start();

    ???

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Forget writing threads for now. Get yourself a decent Java tutorial and learn the basics first including how to create instances and how to call methods.

  5. #5
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default

    I know that well and did it but on threads no compile?
    C:\JavaAdv\Lesson6\Assign61>javac Advertisement.java
    Advertisement.java:41: cannot find symbol
    symbol : variable x
    location: class Advertisement
    public boolean getAd() { return x; }
    ^
    1 error

    C:\JavaAdv\Lesson6\Assign61>

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    There is no "x" variable in scope within the getAd method. Again, I have to agree with r...x as this is a very basic aspect of Java that you need to know before messing with threads. You would benefit greatly by studying a basic Java tutorial.

  7. #7
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default

    basically I want when thread
    class Advertisement extends Thread
    {
    public void run()....
    exits alone
    class TestAdvt
    {
    public static void main (String[] args)
    {
    get terminated, but when exit with advt.interrupt();
    ...
    catch (InterruptedException e)
    {
    break;
    ...
    continue running the threed from beginning ?
    Last edited by lse123; 09-19-2009 at 09:56 PM.

  8. #8
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default

    you mean to transfer vars/methods between classes is the same as normal, in case of Thread classes ?

  9. #9
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default

    must be different since now we have run(), what if a var get it's value from the run() ?

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by lse123 View Post
    must be different since now we have run(), what if a var get it's value from the run() ?
    You are not making any sense. The run method is void.
    Do read the Java basics tutorial. If you already read it(like you claimed above) the read it again because you clearly missed some of the basics.

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 06:37 PM
  2. Replies: 6
    Last Post: 02-22-2009, 07:04 AM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 11:30 PM
  4. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:31 PM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 09:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •