static字段初始化过程与静态内部类

/**
* 无论多少对象,静态数据只占用一份存储区域。static 不能应用于 局部变量
* 因此,它只能作用于域(事实上,static修饰的类似全局变量的概念)。
* 下面的例子,阐明静态存储区域 何时初始化
*/

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 StaticInitialization {
    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();
}

/**
* 总结: 初始化的顺序是先 静态对象,而后是 非静态对象。要执行一个 方法,须加载对应的类(载入className.class),
* 这时,类的静态域首先被初始化,且只执行一次。
* 另外,先执行出现于字段定义处的初始化,后执行构造器。
*/

执行结果

/==============分割线===================/

/**
 * 不需要内部类对象与其外部类有联系,那么就将内部类声明为 static。
 * 普通的内部类对象隐式地保存一个外部类对象的引用, 然而 静态内部类 没有。
 * 意味着:
 * 1)创建static类对象,并不需要外部类对象。
 * 2)不能从static类对象中访问非静态的外部类对象。
 * <p>
 * <p>
 * 静态内部类 与 普通内部类 另一区别:普通内部类不能有 static 数据和字段
 * (除了静态常量表达式),但静态内部可以有。
 */

public class Parcel11 {

    private class Inner {
        private final static int x = 1;

        //以下语句不合法
        //final static Inner a=new Inner();
        //static Inner a1=new Inner();
        //static int y;
    }

    private static class ParcelContents implements Contents {

        private int i = 2;

        @Override
        public int value() {
            return i;
        }
    }


    protected static class ParcelDestination implements Destination {

        private String label;

        private ParcelDestination(String whereTo) {
            label = whereTo;
        }

        @Override
        public String readLabel() {
            return label;
        }

        //静态内部类包含静态元素
        public static void f() {
        }

        static int x = 3;

        static class AnotherLevel {
            public static void f() {
            }

            static int x = 4;
        }
    }


    //提供外部访问

    public static Contents contents() {
        return new ParcelContents();
    }

    public static Destination destination(String s) {
        return new ParcelDestination(s);
    }

    public static void main(String[] args) {
        Contents c = contents();
        System.out.println(c.value());

        Destination d = destination("Wust");
        System.out.println(d.readLabel());

        System.out.println("静态内部类 x = " + ParcelDestination.x);
        System.out.println("静态内部类 的 静态内部类 x = " + ParcelDestination.AnotherLevel.x);

        System.out.println("普通类的静态常量 x = " + Inner.x);
    }

}

执行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值