首页 > 代码库 > static 修饰的变量在程序中容易出现的问题
static 修饰的变量在程序中容易出现的问题
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。