×

Discussion Board

Results 1 to 9 of 9

Thread: Return Instance

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    91

    Return Instance

    Hello,

    First look at the snap of code below.

    PHP Code:

    public class Exp{

      public static 
    void main(String[] str){
         new 
    UseExp();
         
      }
    }

    class 
    UseExp implements Runnable{


      
    Thread thread;

      public 
    UseExp(){

         (
    thread = new Thread(this)).start(); //Works Properly
         //(Thread thread = new Thread(this)).start(); //Prompt Error MSG

      
    }

      public 
    void run(){

         
    System.out.println("In run()");
      }

      


    In first one
    Code:
    (thread = new Thread(this)).start();
    - Return value get assigning in thread instance despite start() is calling
    - No instance is AVAIL to call start(), just VOID

    But Any Way its working, In don't know how ???


    And in second one
    Code:
    (Thread thread = new Thread(this)).start();
    - The only difference is that here we have a local instance instead a global like in a first case
    - Why error messages are coming with it ?


    Hope I've been elaborated my DOUBT here!
    Please let me know if you need any additional information on it.






    Amit

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Return Instance

    Quote Originally Posted by amit_yadav View Post
    Code:
    (thread = new Thread(this)).start();
    An assignment expression (one with "=" in it) evaluates to the value being assigned. So:

    Code:
    // assigns 5 to both i and j
    i = (j = 5);
    So your assertion that "No instance is AVAIL to call start(), just VOID" is not correct. The reference stored in "thread" is used to invoke start().

    Quote Originally Posted by amit_yadav View Post
    Code:
    (Thread thread = new Thread(this)).start();
    This isn't OK, because you can't declare a variable in the middle of an expression. Similarly, you can't:

    Code:
    int i;
    i = (int j = 5);
    It's just not allowed in Java syntax.

    Incidentally, you can:

    Code:
    (new Thread(this)).start();
    If you don't want later access to the Thread object, then there is no rule that says you must assign the reference to a variable at all. The Thread doesn't become garbage, because it's still referenced by the VM itself.

    Graham.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    91

    Re: Return Instance

    Hello Graham,
    First of all thanks for your reply.

    I got it, why the statement below,

    Code:
    //(Thread thread = new Thread(this)).start(); //Prompt Error MSG
    Giving compilation error,
    Originally Posted by Graham
    because you can't declare a variable in the middle of an expression.

    NOTE: I would like to done it in a way people's do. But I don't know how can I refer some one post like,

    Originally Posted by ${user} with link.
    If it possible then please also let me know about this thing.

    But still I have an anxiety about the below statement,

    Code:
    (thread = new Thread(this)).start(); //Works Properly
    See, below statement is working properly
    Code:
    new Thread(this).start(); //Works Properly
    But this statement,
    Code:
    thread = new Thread(this).start(); //Giving compilation error
    Again giving compilation error.

    Could you please explain me about this ?







    Amit.

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: Return Instance

    Code:
    (thread = new Thread(this)).start(); //Works Properly
    --> first a new Thread is created of this, then it is assigned to variable thread, which is started later.

    See, below statement is working properly
    Code:
    new Thread(this).start(); //Works Properly
    a new Thread of this is created and then started.

    But this statement,
    Code:
    thread = new Thread(this).start(); //Giving compilation error
    a new Thread of this is created and then started, but as the start() method of thread class is void and it does not returns an instance of the thread which can be assigned to the variable thread, hence it's a compilation error
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    91

    Thumbs up Re: Return Instance

    Hello Amit/Graham,
    Thanks to both of you, by the below quote posted by Graham,
    An assignment expression (one with "=" in it) evaluates to the value being assigned. So:
    I should have understood prior but I did't.

    After that when Amit replied, I have been checked associativity of '.' operator.
    Which enable me to clear my anxiety.

    At last, if you let me know about


    Originally Posted by Graham
    because you can't declare a variable in the middle of an expression.

    NOTE: I would like to done it in a way people's do. But I don't know how can I refer some one post like,

    Originally Posted by ${user} with link.
    If it possible then please also let me know about this t
    I'll be grateful to you.








    Amit

  6. #6
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: Return Instance

    Quote Originally Posted by amit_yadav View Post
    test message
    If you want above, see the FAQ section - http://discussion.forum.nokia.com/fo...o=bbcode#quote
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    91

    Re: Return Instance

    Hello Amit,
    Thanks a lot, once again to indulge me.





    Amit.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: Return Instance

    Quote Originally Posted by amit_yadav View Post
    Hello Amit,
    Thanks a lot, once again to indulge me.

    Amit.
    Shortcut for what you want to do, just click on Multi-Quote (let it turn red)on the right side before quick reply and then click on Quote button(reply with Quote), you will get this as above.
    Thanks,
    Ekta

  9. #9
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Return Instance

    Quote Originally Posted by amit_yadav View Post
    Code:
    (thread = new Thread(this)).start(); //Works Properly
    new Thread(this).start(); //Works Properly
    thread = new Thread(this).start(); //Giving compilation error
    This is a matter of operator precendence.

    This:

    Code:
    (thread = new Thread(this)).start();
    works fine, because the assignment happens first, evaluating to the object reference. That reference is then used to invoke the start() method.

    However:

    Code:
    thread = new Thread(this).start();
    doesn't work, because the ".start()" happens before the assignment. So, the assignment is from the value returned by start()... well, start() is declared "void" - it doesn't return a value, so there's no value to assign, and the code won't compile.

    Graham,.

Similar Threads

  1. expression syntax error
    By doesitmatter in forum Symbian
    Replies: 19
    Last Post: 2008-08-04, 11:08
  2. a function return value problem
    By letmefly111 in forum Symbian
    Replies: 3
    Last Post: 2007-07-23, 20:50
  3. Replies: 6
    Last Post: 2005-10-03, 05:51
  4. How to make singleton
    By rownak_hasan in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2003-02-06, 23:15

Posting Permissions

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