I\'m getting an error: The method sleep(int) is undefined for the type Thread. I thought the sleep method is in the Thread class in Java.
import java.util.Ra
Answers to this question have already been posted yet one another analogy. It may not be the direct answer but may help you remove your confusion why should avoid reusing the names of platform classes, and never reuse class names from java.lang, because these names are automatically imported everywhere.
Programmers are used to seeing these names in their unqualified form and
naturally assume that these names refer to the familiar classes from java.lang
. If
you reuse one of these names, the unqualified name will refer to the new definition
any time it is used inside its own package.
One name can be used to refer to multiple classes in different packages. The following simple code snippet explores what happens when you reuse a platform class name. What do you
think it does? Look at it. It reuses the String
class from the java.lang
package. Give it a try.
package test;
final class String
{
private final java.lang.String s;
public String(java.lang.String s)
{
this.s = s;
}
@Override
public java.lang.String toString()
{
return s;
}
}
final public class Main
{
public static void main(String[] args)
{
String s = new String("Hello world");
System.out.println(s);
}
}
This program looks simple enough, if a bit repulsive. The class String
in the
unnamed package is simply a wrapper for a java.lang.String
instance. It seems
the program should print Hello world. If you tried to run the program, though,
you found that you could not. The VM emits an error message something like this:
Exception in thread "main" java.lang.NoSuchMethodError: main
If you're using the NetBeans IDE, the program would simply be prevented from running. You would receive the message No main classes found.
The VM can’t find the main
method because it isn’t there. Although
Main
has a method named main
, it has the wrong signature. A main method
must accept a single argument that is an array of strings. What the
VM is struggling to tell us is that Main.main
accepts an array of our String
class, which has nothing whatsoever to do with java.lang.String
.
Conclusion : As mentioned above, always avoid reusing platform class names from the java.lang
package.