为什么在变量已被”final”修饰,防止修改的情况下还要使用”private + final”呢?

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

Why use "private + final" when "final" alone already prevents the variable from being modified?

问题

我看到了许多类似于 private final int variable; 的变量声明。

我在想,我们是否可以将 private 安全地改为 public

我认为我们使用 private 的主要原因是防止其他类修改变量,但是在这里,final 关键字已经防止了任何修改。

所以我猜答案是可以的?

英文:

I saw a lot of variable declarations like private final int variable;

I am wondering if we can safely change private to public?

I think the main reason why we use private is to prevent the variable from being modified by other classes, but here the final keyword already prevents any kind of modification.

So I guess the answer is yes?

答案1

得分: 4

私有性不仅仅是关于无法更改值 - 它还意味着无法从外部看到该值。这是其他类不应依赖的值,要么因为它是实现细节,要么因为它不是该类公开的接口的永久性部分。通过将其设为私有,其他类将无法引用它,在该值更改或完全消失时也不会中断。

英文:

Private isn't about just not being able to change the value- its about not being able to even see it from the outside. It's a value that other classes shouldn't depend on, either because its an implementation detail, or because it's not a permanent part of the interface that class is exposing it. By making it private, other classes won't be able to reference it and won't break when the value changes or goes away entirely.

答案2

得分: 1

封装。

通过使用 private 关键字来定义修饰符,可以防止在类外部访问数据。

如果希望在类外部访问数据,必须定义一个公共方法来访问数据。

编辑:我认为你想知道为什么要使用 private 修饰符。

这是有主观看法的,但在大多数情况下,这就是为什么我们想要实现“封装”。想象一下,你在一个类内部编写一组逻辑。

public class MyClass {
  private final String MyAttribute;

  public MyClass() {
    // 逻辑。/ 初始化 MyAttribute
  }

  public String getMyAttribute() {
    return this.MyAttribute;
  }

  public void setMyAttribute() {
    // 逻辑。
  }
}

显然,你不希望其他代码干扰你的逻辑。否则,可能会导致:1. 安全漏洞,2. 错误。这就是为什么要使用 private 修饰符。通过使用 封装,你定义了其他代码如何与你的类交互(如何获取数据,如何修改数据)。

英文:

Encapsulation.

By defining the modifier with private it prevents the data from being accessed by the code outside of a class.

You have to define a public method on how to access the data if you wish to access the data outside of a class.

Edit: I think you're wondering why would we use private modifier.

This is opiniated but it is why on most case we want to achieve Encapsulation. Think of, you write a set of logic inside a class.

public class MyClass {
  private final String MyAttribute;

  public MyClass() {
    // logic. / initialize the MyAttribute
  }

  public String getMyAttribute() {
    return this.MyAttribute;
  }

  public void setMyAttribute() {
    // logic.
  }
}

Obviously, you don't want other code to mess with your logic. Otherwise, it may lead to: 1. Security vulnerabilities, 2. Bugs. That's why you use the private modifier. To achieve encapsulation / you define on how other code interact (how to get the data, how to modify the data) with your class.

答案3

得分: 1

我想从先前的回答中添加更多细节。

private是访问权限。类外部无法“看到”它。
final是一个声明不可更改的固定值。无论在类的内部还是外部,都不能改变它。

因此,private final的作用是:

  1. 确保变量对外部隐藏。
  2. 该变量拥有一个固定值。
英文:

I want to add more details from the previous answers.

private is accessibility. Outside the class cannot "see" it.
final is a declaration of fixed value. Nothing can change it, both inside and outside the class.

So private final is to:

  1. Make sure the variable is hidden to outside.
  2. The variable has a fixed value.

答案4

得分: 0

简而言之,答案是否定的。private 关键字是 访问修饰符 的一个示例,它控制这个变量是否对其他类可见。我们将变量设为 private,这样它就对其他类隐藏,不能在类外部被读取或修改。

考虑以下示例:

/* Person.java */
public class Person {
    private int bankBalance = 1000;      // 私有成员
}

/* Hacker.java */
public class Hacker {
    public static void main(String[] args) {
        Person Tom = new Person();
        int tomMoney = Tom.bankBalance;  // 错误!
    }
}

Person 类内部,我们将成员变量 bankBalance 设为 private。这意味着 bankBalance 只能在 Person 类内部访问(即可读取和修改)。因此,如果我们在 Hacker 类内部尝试执行 int tomMoney = Tom.bankBalance;,就会报错,提示 The field Person.bankBalance is not visible(Person.bankBalance 字段不可见)。由于 bankBalance 对于 Hacker 类已经是不可见的,这意味着其他类无法修改这个变量。

如果我们以不同方式定义 bankBalance 变量,这些行为就会不同:

+-------------------------------+-----------------------+-----------------------+
|                               |     在 "Person" 内部     |    在 "Person" 外部     |
|                               +----------+------------+----------+------------+
|                               |  可读取  |  可修改   |  可读取  |  可修改   |
+-------------------------------+----------+------------+----------+------------+
| private int bankBalance       |    是    |    是     |   否     |    否      |
+-------------------------------+----------+------------+----------+------------+
| private final int bankBalance |    是    |    否     |   否     |    否      |
+-------------------------------+----------+------------+----------+------------+
| public int bankBalance        |    是    |    是     |   是     |    是      |
+-------------------------------+----------+------------+----------+------------+
| public final int bankBalance  |    是    |    否     |   是     |    否      |
+-------------------------------+----------+------------+----------+------------+
  • 如果 bankBalance 是 private,它在 Person 类外部是不可见的。因此,它在 Person 内部和外部都不能被读取和修改。
  • 如果 bankBalance 是 final,无论在 Person 类内部还是外部,它都不能被修改。
英文:

In short, the answer is no. The private keyword is an example of an access modifier, which controls whether this variable is visible to other classes. We make a variable private such that this variable is hidden from other classes and it cannot be read or modified outside the class it's defined in.

Consider the following example:

/* Person.java */
public class Person {
    private int bankBalance = 1000;      // Private member
}

/* Hacker.java */
public class Hacker {
    public static void main(String[] args) {
        Person Tom = new Person();
        int tomMoney = Tom.bankBalance;  // ERROR!
    }
}

Inside the Person class, we set the member variable bankBalance to be private. This means that bankBalance is only accessible (ie. readable and modifiable) within the Person class. Therefore, if we try to do int tomMoney = Tom.bankBalance; inside the Hacker class, this would give an error saying that The field Person.bankBalance is not visible. Since bankBalance is already invisible to the Hacker class, this implies that this variable cannot be modified by other classes.

These are the behaviors if we define the bankBalance variable differently:

+-------------------------------+-----------------------+-----------------------+
|                               |    Within "Person"    |    Outside "Person"   |
|                               +----------+------------+----------+------------+
|                               | Readable | Modifiable | Readable | Modifiable |
+-------------------------------+----------+------------+----------+------------+
| private int bankBalance       |    Yes   |     Yes    |    No    |     No     |
+-------------------------------+----------+------------+----------+------------+
| private final int bankBalance |    Yes   |     No     |    No    |     No     |
+-------------------------------+----------+------------+----------+------------+
| public int bankBalance        |    Yes   |     Yes    |    Yes   |     Yes    |
+-------------------------------+----------+------------+----------+------------+
| public final int bankBalance  |    Yes   |     No     |    Yes   |     No     |
+-------------------------------+----------+------------+----------+------------+
  • If bankBalance is private, it's invisible outside the Person class. Thus, it must not be readable and modifiable outside Person
  • If bankBalance is final, it must not be modifiable no matter whether this variable is accessed within or outside Person

huangapple
  • 本文由 发表于 2020年10月18日 11:24:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64409412.html
匿名

发表评论

匿名网友

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

确定