本文讨论Java中(静态)变量.(静态)代码块的执行顺序 首先创建3个类: 1.Foo类,用于打印变量 public class Foo { public Foo(String word) { System.out.println(word); } } 2.Parent类 public class Parent { static Foo FOO = new Foo("Parent's static parameter"); Foo foo = new Foo("Parent'…
不多说,直接上干货! 这种形式的程序段我们将其称之为代码块,所谓代码块就是用大括号({})将多行代码封装在一起,形成一个独立的数据体,用于实现特定的算法.一般来说代码块是不能单独运行的,它必须要有运行主体.在Java中代码块主要分为四种: public class Test { { //// } } 普通代码块 普通代码块是我们用得最多的也是最普遍的,它就是在方法名后面用{}括起来的代码段.普通代码块是不能够单独存在的,它必须要紧跟在方法名后面.同时也必须要使用方法名调用它. public cl…
1.构造器:与类同名且没有返回值,用来初始化类属性: 构造器又分为无参构造器和有参构造器 1.1:无参构造器 public class Contruction{ ...属性... public Contruction(){}//无参构造器,不写,系统会自动添加 } 1.2:有参构造器 public class Contruction { private int i; public Contruction( int i){/*有参构造器,如果你定义了一个有参数的构造器,那么你在实例化对象的时候必须…
http://www.cnblogs.com/naruto469/p/3608459.html public class Print { 2 3 public Print(String s){ 4 System.out.print(s + " "); 5 } 6 } 1 public class Test1 { 2 3 public static Print obj1 = new Print("1"); 4 5 public Print obj2 = new Pri…
验证证的方法是写code,如下: public class test { static class A { public static String name = "hello"; static { System.out.println("A static block1"); //1 } { System.out.println("A block3"); //3 } public A() { System.out.println("A…