1 public class StaticTest { 2 3 public static StaticTestClass staticMember = StaticTestClass.newInstance(); 4 public static StaticTestClass staticMember2 = StaticTestClass.newInstance(123); 5 public static int staticMember3 = StaticTestClass.getNumber(); 6 7 public static void main(String[] args) { 8 if (StaticTest.staticMember != null && StaticTest.staticMember.x == 321) { 9 System.out.println("StaticTest.staticMember.x correct: " + StaticTest.staticMember.x); 10 } else { 11 System.out.println("StaticTest.staticMember.x failed!"); 12 } 13 if (StaticTest.staticMember2 != null && StaticTest.staticMember2.x == 123) { 14 System.out.println("StaticTest.staticMember2.x correct: " + StaticTest.staticMember2.x); 15 } else { 16 System.out.println("StaticTest.staticMember2.x failed!"); 17 } 18 if (StaticTest.staticMember3 == 456) { 19 System.out.println("StaticTest.staticMember3 correct: " + StaticTest.staticMember3); 20 } else { 21 System.out.println("StaticTest.staticMember3 failed!"); 22 } 23 } 24 } 25 26 class StaticTestClass { 27 public int x; 28 29 public StaticTestClass() { 30 x = 321; 31 } 32 33 public StaticTestClass(int x) { 34 this.x = x; 35 } 36 37 public static StaticTestClass newInstance() { 38 return new StaticTestClass(); 39 } 40 41 public static StaticTestClass newInstance(int x) { 42 return new StaticTestClass(x); 43 } 44 45 public static int getNumber() { 46 return 456; 47 } 48 } 49 50 // vim: tabstop=4 expandtab shiftwidth=4