Java -The method sleep(int) is undefined for the type Thread

后端 未结 7 2043
南方客
南方客 2020-12-18 14:46

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         


        
相关标签:
7条回答
  • 2020-12-18 15:25

    You implemented your own class called Thread and try to call sleep on it, which fails, cause sleep is undefined in your class. Your class basically shadows java's Thread class.

    Call your class differently (ie. MyThread or even better MyRunnable, as noted by owlstead) or call java.lang.Thread.sleep() directly.

    0 讨论(0)
  • 2020-12-18 15:25

    The reason you're getting this is that you've implemented your own Thread class. In your class there is no sleep method.

    First prize would be to avoid using class names that are part of the standard Java libraries.

    If you insists to keep the names, use java.lang.Thread.sleep(...) to specify that you want the Thread class that Java provides.

    0 讨论(0)
  • 2020-12-18 15:26

    It's not in your Thread class.

    Since you named your class Thread, that's where Java will look for Thread.sleep. If you want the function that's built into Java, try java.lang.Thread.sleep(time);.

    0 讨论(0)
  • 2020-12-18 15:29

    Fully-qualify Thread since you're trying to use java.lang.Thread, not your own.

    0 讨论(0)
  • 2020-12-18 15:30

    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.

    0 讨论(0)
  • 2020-12-18 15:34

    Your class name "Thread" conflicts with the Thread class in Java standard library. Change the name of your class and it will resolve everything.

    0 讨论(0)
提交回复
热议问题