zoukankan      html  css  js  c++  java
  • 静态数据的初始化

    无论创建多少个对象,静态数据都只占用一份存储区域。static关键字不能应用于局部变量,因此它只能作用于域。如果一个域是静态的基本类型域,且也没有对它进行初始化,那么就会获得基本类型的标准初值,如果他是一个对象引用,那么它的默认初始值就是null。

    /**
    * Created by wangw on 2016/2/28.
    */
    public class Demo3 {
    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();
    }
    class Bowl{
    Bowl(int marker){
    System.out.println("marker==="+marker);
    }
    void f(int marker){
    System.out.println("marker1===="+marker);
    }
    }
    class Table{
    static Bowl bowl1 = new Bowl(1);
    Table(){
    System.out.println("Table");
    bowl1.f(1);
    }
    void f2(int marker){
    System.out.println("f2-maker"+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.f(2);
    }
    void f3(int marker){
    System.out.println("f3==marker"+marker);
    }
    static Bowl bowl5 = new Bowl(5);
    }
    输出结果:

    marker===1
    marker===2
    Table
    marker1====1
    marker===4
    marker===5
    marker===3
    Cupboard
    marker1====2
    Creating new Cupboard in main
    marker===3
    Cupboard
    marker1====2
    Creating new Cupboard in main
    marker===3
    Cupboard
    marker1====2
    f2-maker1
    f3==marker1

    初始化的顺序是先静态对象(如果它们尚未因前面的对象创建过程而被初始化),而后是“非静态”对象。



  • 相关阅读:
    模板代码生成器 Template Code Creater
    Oracle编程入门经典 第2章 SQLPlus和基本查询
    Oracle编程入门经典 第5章 体系结构
    数据仓库
    C++ WINDOWS API 第1章 Windows 应用程序开发入门
    C++ WINDOWS API 第2章 Windows API概要
    Oracle编程入门经典 第7章 表
    单交换机VLAN虚拟局域网划分
    Oracle日志文件被误删除
    Oracle编程入门经典 第4章 新9i示例模式
  • 原文地址:https://www.cnblogs.com/james2015/p/5225855.html
Copyright © 2011-2022 走看看