• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread constructor that takes String is not compiling  RSS feed

 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Thread constructor that takes String gives compiler error. How to make it work?

TestThreads.java:5: cannot find symbol
symbol : constructor Thready(java.lang.String)
location: class Thready
Thread tt = new Thready(x);
^
1 error



Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at that line of code and the error message. You've just made a typing error.
 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Look carefully at that line of code and the error message. You've just made a typing error.


I still don't get it. please point out.
changing
Thread tt = new Thready(x);

to

Thread tty = new Thready(x);

still gives the same error
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message tells you what it can't find: a constructor Thready(String). What's a Thready?
 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:The error message tells you what it can't find: a constructor Thready(String). What's a Thready?


Got it,constructors can't be inherited ,now this code works fine
My intention was to demonstrate use of Thread constructor that takes a String as an argument

 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it,constructors can't be inherited


No, the solution was to change Thready to Thread as pointed out by Matthew.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's simpler than that: you were typing Thready instead of Thread. Notice the extra "y".
 
Vijay Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It's simpler than that: you were typing Thready instead of Thread. Notice the extra "y".



Extra 'y' was deliberate
Because I needed a class that was subclass of Thread

Here's the code that I posted before

If I change Thready to Thread in this line:
Thread tt = new Thready(x);

it will compile fine .

But I needed the run method to run as well so that it could show output, though I hadn't mentioned it before


 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay Tyagi wrote:Extra 'y' was deliberate
Because I needed a class that was subclass of Thread

As you've probably realised by now, if you want a named subclass, you've got to actually define it. You can't throw in class (or variable) names that the compiler doesn't know about, because it won't know what to do with it.

So you can either do what you've now done with your example, which is to define a named class and then instantiate it. Or you can do what you did earlier in the thread (with the new Thread() {...} syntax), and create an anonymous subclass of Thread.

Or (which is generally a better solution) you can create a Runnable object (using either the named or anonymous approach), and then pass that into the constructor of a plain Thread object.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!