---
title: "java之对象引用static变量"
author: "yoyo08"
date: "2010-01-18T09:53:26Z"
tags: []
categories: ["编程语言"]
slug: "java-static"
draft: false
lastmod: "2010-01-18T09:53:26Z"
id: 573173
url: "https://yoyo08.iteye.com/blog/573173"
---

如下面代码

 

```
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
```