构造器的作用:确保成员变量得到初始化,在new实例之后,可以直接使用
1>构造器初始化顺序:变量定义的顺序决定了初始化的顺序,他们都会在任何方法(包括构造器)调用之前得到初始化(注意,采用类的形式直接去调用静态方法,此时,普通的成员变量将不会被初始化,因为没有为对象在堆中开辟空间,即只调用类的静态方法时,只会对类的静态变量优先进行初始化,然后执行被调用的静态方法,普通变量并不会进行初始化)
class Display{
Display(int market){
System.out.println("Display"+"("+market+")");
}
}
public class Computer {
Display d1 = new Display(1);
static Display d2 = new Display(2);
static void p(){
System.out.println("Computer static method");
}
Display d3 = new Display(3);
Computer(){
System.out.println("Computer"+"()");
}
public static void main(String[] args) {
//Computer.p(); //注意输出结果的不同
new Computer();
}
}
2>静态数据的初始化:无论你创建多少个对象,静态数据都将在类加载到虚拟机的时候,仅仅执行一次。
class Display{
Display(int market){
System.out.println("Display"+"("+market+")");
}
}
public class Computer {
Display d1 = new Display(1);
Computer(){
System.out.println("Computer"+"()");
}
public static void main(String[] args) {
new Computer();
new Conputer();//只打印一个输出语句
}
}
3>将类加载到虚拟机的时候,一个类中如果有静态域和静态代码块,将会执行这些代码(静态方法可以在静态代码块中得到调用)示例如下:
package com.thinking.in.java.course;
class Display{
Display(int market){
System.out.println("Display"+"("+market+")");
}
}
public class Computer {
Display d1 = new Display(1);
static Display d2 = new Display(2);
static void p(){
System.out.println("Computer static method");
}
static {
p();
}
Display d3 = new Display(3);
Computer(){
System.out.println("Computer"+"()");
}
void f(){}
public static void main(String[] args) throws ClassNotFoundException {
//将类加载到虚拟机中
Class.forName("com.thinking.in.java.course.Computer");
//Computer.p();
//new Computer();
}
}
4>类加载的过程
1,编译Java文件,便于定位出class完整的路径信息
2,将class文件载入虚拟机中,执行有关静态初始化的所有动作;
且只执行一次,按照定义的顺序,静态代码块并无优先权限,静态代码块中可以调用静态方法
3,执行new操作,在堆中为对象分配足够的空间
4,初始化该空间,其中基本类型为默认值,引用类型为null
5,按照变量的定义顺序,执行初始化动作;和普通代码块并无关系;
6,执行构造器方法
class Bowl{
Bowl(int marker){
System.out.println("Bowl(" + marker+")");
}
void f1(int marker){
System.out.println("f1(" + marker + ")");
}
}
class Table{
static Bowl bowl1 = new Bowl(1);
Table(){
System.out.println("Table()");
bowl2.f1(1);
}
void f2(int marker){
System.out.println("f2(" + marker + ")");
}
static Bowl bowl2 = new Bowl(2);
}
class Cupboard{
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
Cupboard(){
System.out.println("Cupboard()");
bowl4.f1(2);
}
void f3(int marker){
System.out.println("f3(" + marker + ")");
}
static Bowl bowl5 = new Bowl(5);
}
public class StaticIninialization{
public static void main(String[] args){
new Cupboard();
System.out.println("==========");
new Cupboard();
System.out.println("==========");
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}
/*output:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Bowl(3)
Cupboard()
f1(2)
==========
Bowl(3)
Cupboard()
f1(2)
==========
f2(1)
f3(1)
*/