Results 1 to 6 of 6
Thread: Threads
- 09-14-2010, 02:19 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 1
- Rep Power
- 0
Threads
Hi, I want to ask whether it is necessary to call the Thread class constructor when extending it in your own class? For example...
Java Code:public class TestThread extends Thread { public TestThread() { super(); ....
I have seen some example which do call super() and others which dont.
Thanks
- 09-14-2010, 02:25 AM #2
As far as I know, you need to call super() when extending Thread. However, another way to approach Threads (which is what I always use) is to implement it as Runnable instead.
This link details reasons why you may prefer to use Runnable, and also explains the process of extending and creating a Thread (using either method): Java Threads Tutorial | Java Beginner
- 09-14-2010, 02:25 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 41
- Rep Power
- 0
You don't need to call the parent class constructor.
- 09-14-2010, 02:26 AM #4
No, I don't think there is any reason to call super.
Try it both ways and see if there is any difference.
- 09-14-2010, 09:15 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
The first statement of the body of a ctor is either a call to any super class ctor or a call to another ctor in the same class (this). If there is no explicit call to a superclass ctor (and no call to another ctor in the same class) the compiler generates a no-args superclass ctor call: super(). If calls to other ctors in the same class cause a cycle the compiler detects it and emits an error message. If a class doesn't contain any ctors the compiler will generate an empty public no-args ctor.
kind regards,
Jos
- 09-21-2010, 03:44 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Threads
By GoLuM83 in forum Advanced JavaReplies: 7Last Post: 03-31-2010, 09:52 PM -
When to use threads
By simorgh in forum Threads and SynchronizationReplies: 2Last Post: 02-12-2010, 08:43 AM -
Threads!
By rameshraj in forum Advanced JavaReplies: 1Last Post: 05-04-2008, 05:11 PM -
Using threads
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 11:25 AM -
Threads
By one198 in forum Threads and SynchronizationReplies: 1Last Post: 11-20-2007, 07:15 PM
Bookmarks