版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37817685/article/details/83446053
/**
*
*/
/**
* @author jueying:
* @version 创建时间:2018-10-27 下午02:02:27
* 类说明
*/
/**
* @author jueying
*
*/
class A {
public int a;
/**
* @return the a
*/
public int getA() {
return a;
}
/**
* @param a the a to set
*/
public void setA(int a) {
this.a = a;
}
}
public class Test9 {
A a;
A a1;
/*
*
*/
public void fun(){
createA(a);
System.out.println(a.getA());//java.lang.NullPointerException
}
public void createA(A b){//同理a指向的对象为空,那么b指向的对象也为空
b=new A();//重新给b赋值指向新的对象
b.setA(1);//b给新对象设置属性,但是引用变量a指向的对象任然为空,所以输出报空指针异常
}
public void fun1(){
a1=new A();
create(a1);
System.out.println(a1.getA());//输出1
}
//这里引用变量a1赋值给了引用变量b,b指向了a1引用的对象地址,通过b去改变对象的属性,那么a1指向的对象属性也发生改变
public void create(A b){
b.setA(1);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test9().fun1();
new Test9().fun();
}
}