ITeye博客

java之对象引用static变量

yoyo08 · 2010-01-18 09:53 · Java · 编程语言 · Markdown

如下面代码

 

public class Test
{
	public static int a = 2;
	
	public static void main(String[] args)
	{
		Test t = new Test();
		
		t = null;
		
		System.out.println(t.a);
	}
}

 

 

输出为:

 

2

 

 

下面的代码

public class Test
{
	public static int a = 2;
	public int b = 3;
	
	public static void main(String[] args)
	{
		Test t = new Test();
		
		t = null;
		
		System.out.println(t.a);
		System.out.println(t.b);
	}
}

 

 

输出为:

2
Exception in thread "main" java.lang.NullPointerException

 

 

再看下面的代码:

 

public class Test
{
	public int a = 3;
	public static int b = 2;
	
	public static void main(String[] args)
	{
		Test t = new Test();
		
		t = null;
		
		System.out.println(t.a);
		System.out.println(t.b);
	}
}

 

 

输出为:

 

Exception in thread "main" java.lang.NullPointerException

评论(2)

  1. #2 楼 · yoyo08 · 2010-01-18 12:16

    achenbj writes:

    :oops: 我的怎么不能运行啊,第一个例子就不行。

    我原来是用eclipse运行的 刚又用cmd运行了下 没问题

  2. #1 楼 · achenbj · 2010-01-18 11:48

    :oops: 我的怎么不能运行啊,第一个例子就不行。

评论功能维护中,当前仅展示历史评论。