Results 1 to 3 of 3
  1. #1
    stefan24 is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default Passing a new object to a constructor please clarifiy the concept for me please.

    Hello,

    I'm hoping a kind soul would help clear up a situation for me?
    I am following a course at the moment and I'm confused by this code,

    ....
    Thread newThread = new Thread(new ThreadClassName, "thread one");
    newThread.start();
    ....

    the aspect I'm struggle with is the declaration of ...(new ThreadClassName, ...
    I understand the constructor needs two arguments for this overloaded constructor a Runnable target and a String name.

    I have been use to the follow way to constructor a thread

    ImplementedClassWithARunnableInterface runClass = new ImplementedClassWithARunnableInterface();
    Thread myThread = new Thread(ImplementedClassWithARunnableInterface);
    myThread.start();

    Why is it possible just to say new then the class name instead of defining a data type and name but just passing a new statement to the constructor.

    Please forgive me if I can't articulate the problem in a clearer and more defined way but this is due to me not understanding the circumstance.

    many thanks

    Stefan

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Hi Stefan,

    As far as i understood your question, ...

    I think the basic point there is whether you need a reference to Runnable object in that method or not. If you need an object reference in that method (e.g. you might need to call a method of that Runnable object), then you may define your data type, keep its reference and just pass that reference to the constructor of the Thread class. But if you don't need reference of the created Runnable object, then there is no reason to have the creating code in a separate line beforehand. So in my opinion it just increases readability by decreasing number of lines to read.

  3. #3
    stefan24 is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default

    hey javabean,

    thanks for your reply and you understood my question even though it was quite badly articulated.

    I am still a bit confused so please bear with me :) the part which is causing the problems is how is it possible to pass an object without a reference to the constructor?

    I'm use to needing to have a fulled defined reference with a datatype and variable name. Just creating an object without a reference wouldn't this be marked to be garage collected due to it not having a reference attached?

    I'm wondering why i'm having trouble with something so simple :)

    many thanks for your help !!!

Similar Threads

  1. Problems passing a scan object to a method
    By xkross in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 03:56 PM
  2. mail concept
    By indirani in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:30 PM
  3. mail concept
    By thamizhisai in forum Advanced Java
    Replies: 4
    Last Post: 04-11-2008, 07:19 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

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