提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在类中使用静态初始化器的示例
/*from 时 代 J a v a 公 众 号 - nowjava.com*/ public class Main { private static int num; // A static initializer. Note the use of the keyword static below. static { num = 1; System.out.println("Inside static initializer."); } // An instance initializer { System.out.println("Inside instance initializer."); } // Constructor public Main() { System.out.println("Inside constructor."); } public static void main(String[] args) { System.out.println("Inside main() #1. num: " + num); /* *来 自 N o w J a v a . c o m - 时代Java */ // Declare a reference variable of the class Main si; System.out.println("Inside main() #2. num: " + num); // Create an object