Java – why not call the constructor of the class that implements the runnable interface?

I try to use the constructor of the class that implements the runnable interface But I was surprised to find that it had never been called The run () method was called, but the constructor was never called I wrote a simple example code to show this phenomenon Can anyone explain why?

public class MyRunner implements Runnable {

    public void MyRunner() {
        System.out.print("Hi I am in the constructor of MyRunner");
    }

    @Override
    public void run() {
        System.out.println("I am in the Run method of MyRunner");
    }

    public static void main(String[] args){
        System.out.println("The main thread has started");
        Thread t = new Thread(new MyRunner());
        t.start();
    }
}

Solution

Change public void myrunner() to public myrunner() (no return type) Public void myrunner() is not a constructor, it is a method Constructor declaration has no return type

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>