Hi
I've typed in the following code from a book I'm reading and it wont
compile. I know the classpath is OK as if I create a new ShowThread()
without a string initialiser then the code compiles. But as soon as I place
a String in there it wont compile. Even though the only constructor I've
given ShowThread takes a String
ShowThread.java
package thready;
public class ShowThread extends Thread {
String message;
/** Creates a new instance of ShowThread */
public ShowThread(String message) {
this.message=message;
}
public void run() {
while(true) {
System.out.println(message);
}
}
}
Main.java
package thready;
import java.lang.*;
public class Main {
/** Creates a new instance of Main */
public Main() {
}
public static void main(String[] args) {
new ShowThread(); // this line compiles ok
new ShowThread("Hello"); // this line gives error
}
}
the error I get for the second instansiation of ShowThread is
cant find symbol
constructor ShowThread(java.lang.String)
location class thready.ShowThread
Why cant the compiler see my constructor with a String argument?
MC - 03 Feb 2006 17:18 GMT
Ok I've realised I forgot to add the start
ie
New ShowThread("Hello").start();
and now it compiles OK but I'm still confused as to why it didn't compile
before. I can see how the compiler might be smart enough to realise that
since I haven't created a reference variable to it that I can never start
it. But if thats what the compiler was complaining about why did it say it
couldnt find the constructor instead of unrunable thread or something?
MC - 03 Feb 2006 18:02 GMT
OK I've reinstalled the jdk and now it copiles ok even without the .start()
call. So no need to help me anymore it was some kind of installation problem