简体   繁体   中英

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

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?

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM