[英]Why an object marked as final can be modified and call non-final method in Java?
I am new to Java and I am from C++ background. 我是Java的新手,我来自C ++背景。
I thought final
in Java works just like const
in C++ but I guess not. 我认为Java中的
final
就像C ++中的const
一样,但我猜不是。
Object initiated as const
in C++, only can call const
methods and cannot change fields in the object. 在C ++中作为
const
启动的对象,只能调用const
方法,不能更改对象中的字段。
But in my code below, I am able to assign value in pet
. 但是在我下面的代码中,我可以为
pet
分配值。 ie pet.id = new ObjectId(newPetId);
即
pet.id = new ObjectId(newPetId);
. 。
private void addPet() {
progressBar.setVisibility(View.VISIBLE);
final Pet pet;
try {
// Locally add and save pet.
pet = getPetFromUserInput();
} catch (InvalidInputException e) {
progressBar.setVisibility(View.GONE);
return;
}
pet.id = new ObjectId(); // Modify field member directly.
pet.updateName("MyPet"); // Call non-final method.
}
Referencing Erik's answer in comments, I found an easy explanation for C++ programmers. 在评论中引用Erik的答案,我找到了一个简单的C ++程序员解释。
Pet pet;
in Java is like Pet* pet;
在Java中就像
Pet* pet;
in C++. 在C ++中。
final Pet pet;
in Java is like Pet * const pet;
在Java中就像
Pet * const pet;
in C++ which makes the pointer const
but not the value itself. 在C ++中,它使指针成为
const
而不是值本身。
Note that there is a subtle difference in Java and C++. 请注意,Java和C ++之间存在细微差别。
In C++, you have to assign a value when declaring a const
variable but in Java, it lets you do it later but only once. 在C ++中,您必须在声明
const
变量时指定一个值,但在Java中,它允许您稍后执行但只执行一次。
In Java, the keyword "final" simply means that once initialized, you cannot change the value of the variable. 在Java中,关键字“final”仅表示初始化后,您无法更改变量的值。 for example,
例如,
final int x = 0;`
//You can't do this!!!
int x=5
It has nothing to do with that variable calling methods. 它与该变量调用方法无关。
In java "final" means this 在java中,“final”意味着这一点
1.If you use "final" before a class, then it means there is no chance to create subclass to that class. 1.如果你在课前使用“final”,那就意味着没有机会为那个类创建子类。
public final class Person {
void getName() {
}
} }
Then you can't create like this. 那你不能这样创造。
public class Man extends Person{
}
"The type Man cannot subclass the final class Person" will be shown
If you write "final" before a method then 如果你在方法之前写“最终”
public class Person { final void getName() { } }
Then you can create subclass to this Person class but you cant override the getName() in subclass. 然后你可以为这个Person类创建子类,但你不能覆盖子类中的getName()。
public class Man extends Person{
@Override
void getName() {
// TODO Auto-generated method stub
super.getName();
}
}
"Cannot override the final method from Person" will be shown.
exammple: exammple:
public class Person {
public final String name;
void getName() {
}
}
then in subclass, you can't modify the value. 然后在子类中,您无法修改该值。
public class Man extends Person{
public void getName() {
name = "child";
}
}
"The final field Person.name cannot be assigned" will be shown
all these will shown in compile time itself. 所有这些都将在编译时自行显示。
Hope this helps you. 希望这对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.