There are two classes that are declared in the file.
That will be displayed after the launch?
Test.java:

class Singleton {
	private static Singleton s = new Singleton();
	protected Singleton() {
		System.out.println("S");
	}
	public static Singleton getInstance() {
		return s;
	}
}
	
public class Test {
	public void method() {
		Singleton.getInstance();
	}
	public static void main(String [] str) {
		new Test();
	}
}
Explanation

Initialization of static fields is done during the initialization of the class. The initialization of the class in its turn is performed at the first reference to the class (creation of the exemplar of the class, a call to the static field or method, etc.)

More details about the initialization of classes can be found in the Java Language Specification: docs.oracle.com/javase/...jls-12.htm

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes