Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2006

Tip: Looking for answers? Try searching our database.

newbie cant find constructor error

Thread view: 
MC - 03 Feb 2006 16:57 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.