java中一个值得注意的问题---类成员初始化与变量初始化的区别

2014-11-23 23:26:25 · 作者: · 浏览: 2

1、code1

class Change
{
	int x;
}
public class JavaTest1
{
	public static void main(String[] agrs)
	{
		Change c=new Change();
		System.out.println("The value of c is "+c.x);
	}
}
\

2、code2

public class JavaTest1
{
	public static void main(String[] agrs)
	{
		int a;
		System.out.println("The value of c is "+a);
	}
}

3、分析

1中与2中的结果完全不同,1中类成员使用new函数时被初始化为0,虽然并没有通过对象进行显示初始化,而2中变量a此时仅仅是一个整形变量的引用,所以会报错