import sdsu.io.Console; class Test { public static void main( String args[] ) { Console.println( "Start Main" ); InitializationTest object; Console.println( "Before creating an object" ); object = new InitializationTest(); Console.println( "a " + object.a ); Console.println( "b " + object.b ); Console.println( "c " + object.c ); Console.println( "d " + object.d ); Console.println( "e " + object.e ); Console.println( "f " + object.f ); } } class StaticPrint { private static int count = 0; public static int print( String message ) { Console.println( message + ", \tvalue " + count); return count++; } }
class InitializationTest { public static int a = StaticPrint.print( "a declaration, static" ); public int b = StaticPrint.print( "b declaration, non static" ); static { a = StaticPrint.print( "a in static block" ); } public static int c = StaticPrint.print( "c declaration, static" ); public int d = StaticPrint.print( "d declaration, non static" ); public InitializationTest() { b = StaticPrint.print( "b in constructor" ); e = StaticPrint.print( "e in constructor" ); } public int e = StaticPrint.print( "e declaration, non static" ); public static int f = StaticPrint.print( "f declaration, static" ); }
a declaration, static, value 0 a in static block, value 1 c declaration, static, value 2 f declaration, static, value 3 b declaration, non static, value 4 d declaration, non static, value 5 e declaration, non static, value 6 b in constructor, value 7 e in constructor, value 8 a 1 b 7 c 2 d 5 e 8 f 3
class ForwardReferenceAndInitialization { public static int first = 1; public static int second = first * 2; public static int third = fourth - 1; // Compiler error public static int fourth = 4; public int fifth = 5; public int sixth = fifth + 1; public int seventh = eighth - 1; // Compiler error public int eighth = 8; }