Order of initialization

Order of initialization

Within a class, the order of initialization is determined by the order that the variables are defined within the class. The variable definitions may be scattered throughout and in between method definitions, but the variable s are initialized before any methods can be called—even the constructor.

在一个class里,变量的初始化顺序是被他们自身的定义顺序来决定的。变量定义即使被方法打散,也会比方法先初始化,即使是构造函数.

String s = "abc";
public MethodInit(String s){
        System.out.println(this.s);
        this.s = s;
        System.out.println(this.s);
    }
public static void main(String[] args) {
        MethodInit mi = new MethodInit("cde");
    }
>>>>>>>>>>>>>>输出结果
abc
cde

下面一个例子:

package com.skyon;
/*
 * :initilization
 * Specifying initial values in a class definition.
 * import static net.mindandview.util.*
 * 
 * */

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()");
    }
    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);
    public 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 StaticInitilization {
    public static void main(String[] args) {
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        table.f2(1);
        cupboard.f3(1);
    }
    static Table table = new Table();
    static Cupboard cupboard = new Cupboard();
}
>>>输出结果:
Bowl(1)
Bowl(2)
Table()
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)

To summarize the process of creating an object, consider a class called Dog:
1. Even though it doesn’t explicitly use the static keyword, the constructor is actually a
static method. So the first time an object of type Dog is created, or the first time a
static method or static field of class Dog is accessed, the Java interpreter must
locate Dog.class , which it does by searching through the classpath.

  1. As Dog.class is loaded (creating a Class object, which you’ll learn about later), all of
    its static initializers are run. Thus, static initialization takes place only once, as the
    Class object is loaded for the first time.

  2. When you create a new Dog( ), the construction process for a Dog object first
    allocates enough storage for a Dog object on the heap.

  3. This storage is wiped to zero, automatically setting all the primitives in that Dog
    object to their default values (zero for numbers and the equivalent for boolean and
    char) and the references to null.

  4. Any initializations that occur at the point of field definition are executed.

  5. Constructors are executed.

译:
总结一下创建一个Object的过程,假设有一个类叫做dog:
1. 尽管没有显示声明static关键字,构造函数其实是一个静态方法。所以第一次一个dog对象呗创建的时候,或者第一个需要访问它的静态属性或者静态方法时,JVM就必须定为到Dog.class,通过classpath来搜索。
2. 当Dog.class加载后,所有静态实例化就会执行。但是,只是执行一次,像Class对象第一次被加载时一样。
3. 当你创建一个实例时,构造进程会为dog这个实例分配足够的存储空间。
4. 存储空间降为0, 自动设置所有基本类型的值和引用为null
5. field变量的定义开始执行
6. 构造函数开始执行。

NOTE THAT:
如果是继承的话,那么实例化的顺序为顶层父类的静态变量、依次向下的静态变量,然后是父类的普通子类实例化、构造方法,依次向下。

例子:

package com.skyon;

class Insect{
    private int i = 9;
    protected int j;
    Insect(){
        System.out.println("i =" + i + ", j = " + j);
    }
    private static int x1 = printInit("static Insect.x1 initilized");

    static int printInit(String s){
        System.out.println(s);
        return 47;
    }
}

public class Beetle extends Insect{
    private int k = printInit("Bettle.k initilized");
    public Beetle(){
        System.out.println("k =" + k);
        System.out.println("j =" + j);
    }
    private static int x2 = printInit("static Bettle.x2 initilized");

    public static void main(String[] args) {
        System.out.println("Bettle constructor");
        Beetle b = new Beetle();
    }
}

输出:

static Insect.x1 initilized
static Bettle.x2 initilized
Bettle constructor
i =9, j = 0
Bettle.k initilized
k =47
j =0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Thinking in Java》是由Bruce Eckel撰写的一本Java编程的经典教材,该书以英文语言出版。以下是该书的目录大纲: Chapter 1: Introduction to Objects - Introduction - The progress of abstraction - How objects communicate - Every object has an interface - The process of object-oriented design - Implementation hiding - Inheritance and reuse - Polymorphism and dynamic binding - Summary of object-oriented principles Chapter 2: Everything Is an Object - Primitive types - How the objects are created - Aliasing: All arguments are passed by value - Documentation comments - Controlling access to members of a class - First exposure to Java syntax Chapter 3: Operators - Thinking recursively - Operator precedence - Assignment with objects - Mathematical operators and precedence - Autoboxing and unboxing - Increment and decrement - Java logic operators - Bitwise operators - The instanceof operator - Summary of operators Chapter 4: Control Structures: Part 1 - True and false - The if-else statement - The switch statement - The while statement - The do-while statement - The for statement - Summary of control structures Chapter 5: Control Structures: Part 2 - The break and continue keywords - Foreach and multidimensional arrays - Using the comma operator - The return keyword - Summary of control structures Chapter 6: Initialization & Cleanup - Member initialization - Constructor initialization - Method overloading & generic methods - Order of initialization - Constructor & parameter lists - Garbage collection - The finalize() method - Summary of initialization and cleanup ......(接下去的章节继续)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值