What will be the output of the following code:

public class Main extends Thread {
	 private int a;

	 public Main() {
		 a = 2;
		 start();
		 System.out.println(a);
	 }
	 
	 public void run() {
		 a++;
	 }
	 public static void main( String[] args ) {
		 new Main();
	 }
 }
Explanation
The start() method creates new thread and calls the run() method.
Actually, start() method tells to scheduler to create and run a new thread.
But it's unknown when the execution will start.
So the answer is 'the result is undetermined as it may vary from one execution to another'.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback