初始化

作者: zlb | 来源:发表于2016-02-20 19:45 被阅读25次
  • 初始化顺序
    在类的内部,变量的定义先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间。他们任就会在任何方法(包括构造器)被调用之前得到初始化
 public class init {
    public static void main(String[] args) {
        House house = new House();
        house.print();
    }
}
class Window{
    Window(int math){
        System.out.println("Window:\t"+math);
    }
}
class House{
    Window window1 = new Window(1);
    House(){
        System.out.println("House");
        window3 = new Window(4);
    }
    Window window2 = new Window(2);
    void print(){
        System.out.println("print()");
    }
    Window window3 = new Window(3);
}

结果:
Window: 1
Window: 2
Window: 3
House
Window: 4
print()

  • 静态变量的初始化
    无论创建多少个变量 静态数据都只占用一份存储区域 static 不能应用于局部变量 因此他只能作用于域
 public class InitStatic {
    public static void main(String[] args) {
        System.out.println("start print method Cupboard()");
        new Cupboard();
        System.out.println("start print method Cupboard()");
        new Cupboard();
    }
    //首先执行
    static Table table = new Table();
    static Cupboard cupboard = new Cupboard();
} 
class Bowl{
    Bowl(int math){
        System.out.println("Bowl()\t"+math);
    }
    void print1(int math){
        System.out.println("print()\t"+math);
    }
}
class Table{
    static Bowl bowl1 = new Bowl(1);
    Table(){
        System.out.println("Table()");
    }
    void print2(int math){
        System.out.println("print2()\t"+math);
        bowl2.print1(1);
    }
    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.print1(2);
    }
    void print3(int math){
        System.out.println("print3()\t"+math);
    }
    static Bowl bowl5 = new Bowl(5);
}

结果是:
Bowl() 1
Bowl() 2
Table()
Bowl() 4
Bowl() 5
Bowl() 3
Cupboard()
print() 2
start print method Cupboard()
Bowl() 3
Cupboard()
print() 2
start print method Cupboard()
Bowl() 3
Cupboard()
print() 2

  • 显式的静态初始化
    java允许将多个静态初始化动作组织成一个特殊的“静态子句”(也叫静态代码块),与其他的静态初始化动作一样,这段代码仅执行一次:当首次生成这个类的对象时,或者首此访问属于哪个类的静态数据成员时
class Cup{
    Cup(){
          System.out.println("Cup()");
}
}
 class Cups{
      static Cup cup1;
      static Cup cup2;
       static{
        cup1 = new Cup(1);
         cup1 = new Cup(2);
    }

}
  • 非静态实例实例初始化(代码块初始化)
    与静态初始化一样,只是少了一个static,代码块会在构造器执行之前先执行
class Cup{
    Cup(){
          System.out.println("Cup()");
}
}
 class Cups{
      Cup cup1;
      Cup cup2;
       {
        cup1 = new Cup(1);
         cup1 = new Cup(2);
      }

}

相关文章

  • Swift 5.1 (14) - 初始化和反初始化

    Swift 5.1 (14) - 初始化和反初始化Swift 5.1 (14) - 初始化和反初始化

  • 字符串初始化方式比较

    初始化方法一: 用new String初始化的存储方式: 初始化方法2: 用"="初始化的存储方式: 此方法初始化...

  • 2020-07-21 类属性和对象属性 初始化

    对象属性初始化有3种方式: 声明对象属性时初始化 在构造方法中初始化 在初始化块中初始化 类属性初始化有2种方式:...

  • Swift5.1学习随笔之初始化器

    初始化器 类、结构体、枚举都可以定义初始化器(init) 类有2种初始化器:指定初始化器、便捷初始化器 指定初始化...

  • javaSE回顾_05

    数组: 静态初始化: //静态初始化 int[] a = {1,2,3}; 动态初始化: //动态初始化 int[...

  • 第3章 标准库类型string、vector、数组

    1.string的几种初始化方式 直接初始化与拷贝初始化拷贝初始化使用=,而直接初始化不使用 2. cbegin ...

  • 重捡Java(十二)类与对象 属性初始化

    对象属性初始化 对象属性初始化有3种1. 声明该属性的时候初始化2. 构造方法中初始化3. 初始化块 类属性初始化...

  • 十二、初始化

    初始化 类、结构体、枚举都可以定义初始化器 (本章主要讲类的初始化) 类有2种初始化器 指定初始化器 便捷初始化器...

  • [C++之旅] 11 初始化列表

    [C++之旅] 11 初始化列表 初始化列表的特性 初始化列表先于构造函数执行 初始化列表只能用于构造函数 初始化...

  • Swift的初始化

    一、初始化器 指定初始化器 1、指定初始化器是主要的初始化器,每个类至少有一个指定初始化器。2、默认初始化器总是类...

网友评论

      本文标题:初始化

      本文链接:https://www.haomeiwen.com/subject/bvpvkttx.html