What will be the output of the following code:

public class Main extends Thread {
	 private int a;

	 public Main() {
		 a = 2;
	 public void run() {
	 public static void main( String[] args ) {
		 new Main();
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
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes