Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-09-2007, 04:00 PM
Member
 
Join Date: Jul 2007
Posts: 3
stefan24 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-09-2007, 04:18 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-09-2007, 06:01 PM
Member
 
Join Date: Jul 2007
Posts: 3
stefan24 is on a distinguished road
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 !!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems passing a scan object to a method xkross New To Java 4 04-16-2008 04:56 PM
mail concept indirani New To Java 3 04-16-2008 02:30 PM
mail concept thamizhisai Advanced Java 4 04-11-2008 08:19 AM
Calling constructor of parent class from a constructor Java Tip Java Tips 0 12-19-2007 10:10 AM
Calling constructor of same class from a constructor Java Tip Java Tips 0 12-19-2007 10:01 AM


All times are GMT +3. The time now is 06:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org