简体   繁体   English

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

[英]what happens if two objects acess the same instance variable?

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??我已经创建了一个用户定义的 object,它访问一个实例变量和一个方法。然后我创建了一个由 3 个元素组成的数组,其中一个引用变量引用了我最初创建的 object。现在 ZA2F2ED4F8EBC2CBB4DZ21A9 的实例变量会发生什么情况参考引用相同的 object。为什么它的 null 说 ruff?

CODE:
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");
}


}

OUTPUT:

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

Because you are calling dog1.bark() before setting dog1.name , so name is null when calling dog1.bark() .因为您在设置dog1.name之前调用dog1.bark() ,所以调用dog1.bark()name为 null 。 That's why dog1.bark() prints null for name这就是为什么dog1.bark()打印null作为名称

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

If you set dog1.name before like this如果你像这样设置dog1.name

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

Then it will print Brat says Ruff然后它会打印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. 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"因此,在第二个代码片段中,您正在创建 object 之后立即分配“Brat”,然后调用 dog1.bark(),因此在这种情况下,您在 bark() function 中的逻辑将更改字符串名称= null -----> 默认值为name = "Brat" ,这就是为什么你会得到一个 output 的 "Brat said Ruff"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在for循环中使用相同的变量名初始化多个对象时会发生什么? - What happens when initializing multiple objects in a for loop with the same variable name? 如果两个不同的对象具有相同的哈希码,会发生什么? - What happens if two different objects have the same hashcode? 即使之前初始化了一个同名的实例变量,当我在方法中再次初始化时会发生什么 - what happens when i initialize again in method even though previously initialized a instance variable with same name Java如果将两个对象分配给同一个变量会发生什么 - Java What will happen if we assigned two objects to the same variable 初始化未初始化的实例变量时会发生什么? - What really happens when an uninitialized instance variable is initialized? 如何使用泛型结合两个包含相同类型的HashMap对象,并控制重复的情况 - How can I combine two HashMap objects containing the same types using Generics and control what happens in case of duplicates 什么是成员变量? 它和实例变量一样吗? - What is a member variable? Is it the same as an instance variable? 两个具有相同方法名称的接口,当我覆盖时会发生什么? - Two interfaces with same method name, what happens when I override? 当两个不同的注释具有相同的名称时会发生什么? - What happens when two different annotations have the same name? Java 9 + Maven:如果两个依赖项导出同一个模块会发生什么? - Java 9 + Maven: What happens if two dependencies export the same module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM