i have the following snipet of code :
我有以下snipet代码:
class Phone {
String phoneNumber = "123456789";
void setNumber () {
String phoneNumber;
phoneNumber = "987654321";
}
}
class TestPhone {
public static void main(String[] args) {
Phone p1 = new Phone();
p1.setNumber();
System.out.println (p1.phoneNumber);
}
}
im expecting "987654321" as result, but im getting "123456789" it is like the method setNumber is without any effect can anyone helps me to understand please
我期待“987654321”作为结果,但我得到“123456789”它就像方法setNumber没有任何影响任何人都可以帮助我理解请
4 个解决方案
#1
9
Your re-declaring the phoneNumber variable inside the method, shadowing the field in the class, and so any changes made to the local variable will not be seen in the shadowed class field. Don't do this; get rid of the repeat variable declaration, so that changes made within the method will be seen in the field.
您在方法中重新声明phoneNumber变量,遮蔽类中的字段,因此在阴影类字段中将看不到对局部变量所做的任何更改。不要这样做;摆脱重复变量声明,以便在该字段中看到方法内所做的更改。
e.g., change this:
例如,改变这个:
void setNumber () {
String phoneNumber; // *** this is a local variable, visible ONLY in the method!
phoneNumber = "987654321"; // this has no effect on the field
}
to this:
void setNumber () {
// String phoneNumber;
phoneNumber = "987654321"; // this will change the field!
}
#2
2
In addition to @Hovercraft Full Of Eels
除了@Hovercraft Full Of Eels
void setNumber () {
String phoneNumber;
phoneNumber = "987654321";
this.phoneNumber = "9876"; //this will change your field and give expected result
}
is another way to guarantee to set value of field.
是另一种保证设定领域价值的方法。
In long methods local variable could shadow the field without being aware so there may be local variables or not, always using
this
is a good point to avoid shadowing在long方法中,局部变量可以在不知道的情况下遮蔽字段,因此可能存在局部变量,总是使用这是避免阴影的好点
#3
0
Your variable inside the method is shadowing the class variable...
方法中的变量是阴影类变量...
that is the reason of the weird beha...
这就是怪异行为的原因......
#4
0
As already answered by other users, you're shadowing the variable phoneNumber
by declaring it again in the scope of setNumber
method.
正如其他用户已经回答的那样,您通过在setNumber方法的范围内再次声明它来隐藏变量phoneNumber。
The variable phoneNumber
inside setNumber
is not known outside its scope, and changing it doesn't affect the different phoneNumber
variable declared as a class member.
setNumber中的变量phoneNumber在其范围之外是未知的,并且更改它不会影响声明为类成员的不同phoneNumber变量。
That's why it's a good practice to refer to class members using this
explicitly.
这就是为什么明确地使用它来引用类成员是一个好习惯。
Side note: Try to use access modifiers instead of leaving it naked (no modifier has a class and package access level). As a rule of thumb, always begin with the most restrictive access modifier, and expand it only when you must.
附注:尝试使用访问修饰符而不是让它裸露(没有修饰符具有类和包访问级别)。根据经验,始终从限制性最强的访问修饰符开始,并仅在必要时展开它。
#1
9
Your re-declaring the phoneNumber variable inside the method, shadowing the field in the class, and so any changes made to the local variable will not be seen in the shadowed class field. Don't do this; get rid of the repeat variable declaration, so that changes made within the method will be seen in the field.
您在方法中重新声明phoneNumber变量,遮蔽类中的字段,因此在阴影类字段中将看不到对局部变量所做的任何更改。不要这样做;摆脱重复变量声明,以便在该字段中看到方法内所做的更改。
e.g., change this:
例如,改变这个:
void setNumber () {
String phoneNumber; // *** this is a local variable, visible ONLY in the method!
phoneNumber = "987654321"; // this has no effect on the field
}
to this:
void setNumber () {
// String phoneNumber;
phoneNumber = "987654321"; // this will change the field!
}
#2
2
In addition to @Hovercraft Full Of Eels
除了@Hovercraft Full Of Eels
void setNumber () {
String phoneNumber;
phoneNumber = "987654321";
this.phoneNumber = "9876"; //this will change your field and give expected result
}
is another way to guarantee to set value of field.
是另一种保证设定领域价值的方法。
In long methods local variable could shadow the field without being aware so there may be local variables or not, always using
this
is a good point to avoid shadowing在long方法中,局部变量可以在不知道的情况下遮蔽字段,因此可能存在局部变量,总是使用这是避免阴影的好点
#3
0
Your variable inside the method is shadowing the class variable...
方法中的变量是阴影类变量...
that is the reason of the weird beha...
这就是怪异行为的原因......
#4
0
As already answered by other users, you're shadowing the variable phoneNumber
by declaring it again in the scope of setNumber
method.
正如其他用户已经回答的那样,您通过在setNumber方法的范围内再次声明它来隐藏变量phoneNumber。
The variable phoneNumber
inside setNumber
is not known outside its scope, and changing it doesn't affect the different phoneNumber
variable declared as a class member.
setNumber中的变量phoneNumber在其范围之外是未知的,并且更改它不会影响声明为类成员的不同phoneNumber变量。
That's why it's a good practice to refer to class members using this
explicitly.
这就是为什么明确地使用它来引用类成员是一个好习惯。
Side note: Try to use access modifiers instead of leaving it naked (no modifier has a class and package access level). As a rule of thumb, always begin with the most restrictive access modifier, and expand it only when you must.
附注:尝试使用访问修饰符而不是让它裸露(没有修饰符具有类和包访问级别)。根据经验,始终从限制性最强的访问修饰符开始,并仅在必要时展开它。