What will be the output of the following Java code? [code] class newthread extends Thread { newthread() { super("My

Trung Minh

New member
What will be the output of the following Java code?
Code:
    class newthread extends Thread 
    {
	newthread()
        {
	    super("My Thread");
	    start();
	}
	public void run()
        {
	    System.out.println(this);
	}
    }
    class multithreaded_programing
    {
        public static void main(String args[])
        {
            new newthread();        
        }
    }
A. My Thread
B. Thread[My Thread,5,main]
C. Compilation Error
D. Runtime Error
 
Answer: b
Explanation: Although we have not created any object of thread class still we can make a thread pointing to main method, we can refer it by using this.
Output:
Code:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main].
 

Tìm kiếm

Back
Top