public class JaDemo5 { public static void main(String[] args) { ThisDemo5 t1 = new ThisDemo5(); ThisDemo5 t2 = new ThisDemo5(); Dog d = Dog.getDog(); Dj j = Dj.getDj(); } } class ThisDemo5{ public static String sb = "12345"; { System.out.println("sb"+sb); } public ThisDemo5(){ System.out.println("dsb");//有参构造 } static{ System.out.println("nsdsb"); } } class Dog{ public static Dog dog = null; private Dog(){ System.out.println("dsbd"); } public static Dog getDog(){ if (dog == null){ dog = new Dog(); } return dog; } } class Dj{ private static Dj dj = new Dj(); private Dj(){ //无参构造 } public static Dj getDj(){ return dj; } }
在调用方法时都会对构造函数进行调用