首页 > 其他 > 详细

static 修饰的变量在程序中容易出现的问题

时间:2014-07-22 22:53:04      阅读:318      评论:0      收藏:0      [点我收藏+]

package lianxi;

public class StaticTest {
    int a = 0;
    static int b =0;
    StaticTest(){
        a++;
        b++;//
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StaticTest st = new StaticTest();
        System.out.println("a="+st.a+"............b="+st.b);
        StaticTest st2 = new StaticTest();
        System.out.println("a="+st2.a+"............b="+st2.b);
        
        //被Static 修饰的变量相当于类属性,这里a是属于对象的,每创建对象,a的值就会再次被
        //初始化,而B不同,b属于类

    }

}

=================运行结果==============================

a=1............b=1
a=1............b=2

static 修饰的变量在程序中容易出现的问题,布布扣,bubuko.com

static 修饰的变量在程序中容易出现的问题

原文:http://www.cnblogs.com/siashan/p/3849248.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!