Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
Like Tree4Likes

Thread: The job of a constructor

  1. #21
    Icandoit is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Re: The job of a constructor

    Quote Originally Posted by SurfMan View Post
    Next time don't bother. Your code has NOTHING to do with Threads nor the Runnable interface. The fact that you have a run() method doesn't make you implement Runnable.
    I hope you don't think that I made that post thinking that just because I had public void run I thought it had relation to Runnable.

    No, if look at the question on the original post

    But why are we passing the object which has the implementation of run() (i.e. the object created by new HelloRunnable() ) to the constructor Thread() responsible for only creating a Thread object? What does the Thread() constructor do with the object having the implementation of run()?
    Are questions that should receive a more generic answer not one limited to only an example with the Thread & Runnable classes.

    He wanted to know the use of passing in a Runnable object in Thread's constructor. What my post's purpose was to show him what essentially he's doing when he can see the code in ALL the classes.

    It's polymorphism at its core

    Moreover, I don't think OP is confused as to why you pass it in, I believe he's confused as to why you don't have an instance method to take care of it.

    E.G.

    Java Code:
    Thread t = new Thread();
    t.setRun(new HelloRunnable());
    t.start();
    Which I'm not 100% sure about

    Quote Originally Posted by gimbal2 View Post
    Does that even compile? I hope not.
    It compiles, but ya
    Last edited by Icandoit; 03-17-2014 at 01:28 PM.

  2. #22
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

    Default Re: The job of a constructor

    Quote Originally Posted by Icandoit View Post
    I hope you don't think that I made that post thinking that just because I had public void run I thought it had relation to Runnable.
    Next time, please start your comment with something along the lines of "I'll give you a more generic example.". Because you used run(), it was confusing as hell. You even got me. With your additional explanation, it made sense purposewise.

    Thanks for the link to the polymorphism page at Oracle's. After 15 years of Java programming, I finally found the missing link and broke free from coding everything in public void main(). /sarcasm :)

    Cheers!
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #23
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: The job of a constructor

    Quote Originally Posted by SurfMan View Post
    You even got me.
    Can I get a picture of you I can hang on my wall you most awesome of programmers?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #24
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

    Default Re: The job of a constructor

    Quote Originally Posted by gimbal2 View Post
    Can I get a picture of you I can hang on my wall you most awesome of programmers?
    You probably already have. Pick up the book "History of Java". I should be somewhere between the chapters of some J. Gosling and J. Bloch.

    /sarcasm^2
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #25
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: The job of a constructor

    Sarcasm stack overflow!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  2. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 09:17 AM
  3. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  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
  •