美文网首页
Java变量作用域与生命周期

Java变量作用域与生命周期

作者: 烟雨江南wuwei | 来源:发表于2017-12-14 20:45 被阅读0次

    public class A {

    private int a;

    public A(int a) {this.a= a;}

    protected void finalize() {System.out.println("开始清理对象"+a);}

    }

    (1)public class Test1 {

    public static void main(String[] args) {

    {

    A a = new A(1);

    A aa = new A(11);

    }

    System.gc();

    }

    }

    无输出结果

    ================================================

    (2)public class Test2 {

    public static void main(String[] args) {

    {

    A a = new A(1);

    A aa = new A(11);

    }

    {

    A a = new A(2);

    }

    System.gc();

    }

    }

    输出结果:

    开始清理对象1

    ===================================================

    (3)public class Test3 {

    public static void main(String[] args) {

    {

    A a =new A(1);

    A aa =new A(11);

    }

    {

    A a =new A(2);

    A aa =new A(22);

    }

    System.gc();

    }

    }

    输出结果:

    开始清理对象11

    开始清理对象1

    ==================================================

    (4)public class Test4 {

    public static void main(String[] args) {

    {

    A a =new A(1);

    A aa =new A(11);

    }

    {

    A a =new A(2);

    A aa =new A(22);

    }

    A a =new A(3);

    System.gc();

    }

    }

    输出结果:

    开始清理对象2

    开始清理对象11

    开始清理对象1

    =================================================

    (5)public class Test5 {

    public static void main(String[] args) {

    {

    A a =new A(1);

    A aa =new A(11);

    }

    {

    A a =new A(2);

    A aa =new A(22);

    }

    A a =new A(3);

    A aa =new A(33);

    System.gc();

    }

    }

    输出结果:

    开始清理对象22

    开始清理对象2

    开始清理对象11

    开始清理对象1

    相关文章

      网友评论

          本文标题:Java变量作用域与生命周期

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