如果两个对象访问同一个实例变量会发生什么?

huangapple go评论75阅读模式
英文:

what happens if two objects acess the same instance variable?

问题

我创建了一个用户定义的对象,该对象访问一个实例变量和一个方法。然后我创建了一个由3个元素组成的数组,其中一个引用变量具有对我最初创建的对象的引用。现在,如果两个引用都引用同一个对象,类的实例变量会发生什么?为什么它会显示 null,然后又说 Ruff 呢?

    代码:
    public class Dog{
    String name;
    public static void main(String[] args){
    
    Dog dog1=new Dog();
    dog1.bark();
    dog1.name="Brat";
    
    
    Dog myDogs[]=new Dog[3];
    myDogs[0]=new Dog();
    myDogs[1]=new Dog();
    myDogs[2]=dog1;
    
    
    myDogs[0].name="Fred";
    myDogs[1].name="Marge";
    
    System.out.print("last dogs name is ");
    System.out.println(myDogs[2].name);
    
    int x=0;
    while(x< myDogs.length){
    myDogs[x].bark();
    x=x+1;
    }
    
    
    
    }
    
    public void bark(){
    System.out.println(name+" says Ruff");
    }
    
    
    }
    
    输出:
    
    null says Ruff
    last dogs name is brat
    Fred says Ruff
    Marge says Ruff
    brat says Ruff
英文:

i have created an user defined object which accesses an instance variable and a method.Then i created
an array of of 3 elements in which one reference variable has a reference to object that i initially created.now what happens to instance variable of class if both references refer to same object.why its null says ruff??

CODE:
public class Dog{
String name;
public static void main(String[] args){

Dog dog1=new Dog();
dog1.bark();
dog1.name=&quot;Brat&quot;;


Dog myDogs[]=new Dog[3];
myDogs[0]=new Dog();
myDogs[1]=new Dog();
myDogs[2]=dog1;


myDogs[0].name=&quot;Fred&quot;;
myDogs[1].name=&quot;Marge&quot;;

System.out.print(&quot;last dogs name is &quot;);
System.out.println(myDogs[2].name);

int x=0;
while(x&lt; myDogs.length){
myDogs[x].bark();
x=x+1;
}



}

public void bark(){
System.out.println(name+&quot; says Ruff&quot;);
}


}

OUTPUT:

null says Ruff
last dogs name is brat
Fred says Ruff
Marge says Ruff
brat says Ruff

答案1

得分: 0

因为在设置 dog1.name 之前调用了 dog1.bark(),所以在调用 dog1.bark()name 为 null。这就是为什么 dog1.bark() 打印出 null

Dog dog1 = new Dog();
dog1.bark();
dog1.name = "Brat";

如果你像这样在之前设置了 dog1.name

Dog dog1 = new Dog();
dog1.name = "Brat";
dog1.bark();

那么它会打印出 Brat says Ruff

英文:

Because you are calling dog1.bark() before setting dog1.name, so name is null when calling dog1.bark(). That's why dog1.bark() prints null for name

Dog dog1=new Dog();
dog1.bark();
dog1.name=&quot;Brat&quot;;

If you set dog1.name before like this

Dog dog1=new Dog();
dog1.name=&quot;Brat&quot;;
dog1.bark();

Then it will print Brat says Ruff

答案2

得分: 0

问题是在设置类属性 String name 的值之前调用了方法 dog1.bark(),所以如果在创建对象之前不为类属性初始化一个值,Java 将自动分配默认值。因此,像 String name 这样的对象引用将被初始化为 null。这就是为什么你得到的输出为 null。

所以在第二个代码片段中,你所做的是在对象创建后立即赋值为 "Brat",然后调用 dog1.bark(),因此在这种情况下,bark() 函数内的逻辑将会将 String name = null -----> 默认值 改变为 name = "Brat",这就是为什么你得到一个输出 "Brat says Ruff"。

英文:

Issue is you are calling the method dog1.bark() before setting a value for the class property String name ,so if you don't initialize a value for a class property before the creation of the object java will automatically assign a default value.So Object references like String name will be initialized with null.So that's why you are getting the output as null for the name.

So in the second code snippet what you are doing is you are assigning "Brat" right after the object creation then you are calling the dog1.bark() ,so in this situation your logic inside the bark() function will change the String name = null -----> default value to name = "Brat" ,that's why you are getting an output of "Brat says Ruff"

huangapple
  • 本文由 发表于 2020年7月25日 14:34:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63085179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定