-1

public class Thread{

public static void main(String[] args){
    
    System.out.println(Thread.currentThread().getName());
    
}

}

Thread.java:9: error: cannot find symbol

    System.out.println(Thread.currentThread().getName());

                             ^

symbol: method currentThread() location: class Thread 1 error

  • 1
    your class is named `Thread`, it does not have the static method `currentThread()` - that would be in the class `java.lang.Thread`, but your class is hiding it - change the name of your class (it is not a good idea, not recommended to use same name as standard classes) {you could also use `java.lang.Thread.currentThread()`} – user16320675 Mar 05 '22 at 08:37

1 Answers1

0

user16320675 is correctly pointing out. It is happening because you named your class as "Thread", to solve this problem, either rename your class (eg. to MyThread or Test etc) Or use java.lang.Thread for invoking currentThread() Method on it (to use java's Thread class instead of yours). Find the code below :

public class MyThread{
   public static void main(String[] args){      
     System.out.println(Thread.currentThread().getName());      
   }
}

Or

public class Thread{
    public static void main(String[] args){     
       System.out.println(java.lang.Thread.currentThread().getName());      
    }
}
Yogesh Sharma
  • 280
  • 1
  • 13