This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

preface

  • In Java development, we create a space in memory for things that change frequently. The description of this address is abstractly called a variable
  • But also in Java we sometimes need a variable that doesn’t change the content from beginning to end
  • What good is that? Such variables can be used for global purposes. As a default value

Problem description

  • Final modifiers cannot be modified, which is Java’s positioning of final functionality
  • Final can primarily modify classes, methods, and variables. When a modifier on a class indicates that the class cannot be integrated; When modifier on a method means that the method cannot be overridden; A modifier on a variable cannot be modified. This is what you memorized in school. Can’t go wrong
  • However, careless I began to make confused.
  • There’s a class in the system calleduser. The purpose of the final modifier is to leave the content unchanged and use the default value
final User user = new User("hello");
Copy the code
  • However, when we fetch it somewhere in the program, we find that the final modified User is not necessarily Hello. It could be something else.
  • Well, if it’s possible, then it’s been modified by someone, somewhere.

Problem analysis

  • To resurface the question. I’m going to modify the User object directly after finall.
public static void main(String[] args) {
    final User user = new User("hello");
    user.setName("zxhtom");
    System.out.println(user.getName());
}
Copy the code
  • Based on what we learned, this is when we know we should report an error. But to my surprise, not only was there no error, but the printed content was ZXhTom.

  • Why is this so? The foundation that this is about to pursue oneself did not learn well

  • Final Although modifier variables are variables that cannot be changed. That’s when we only learned and memorized the first half of the sentence. Final modifies variables in exactly the case.

    • Final modifies the underlying variable and its value cannot be changed
    • Final modifies reference type data and its reference address cannot be changed
  • Enjoy the above two sentences. User is a reference type which means that the heap memory address to which a User object modified by final cannot be changed. But the contents of this memory can be changed.

public static void main(String[] args) {
    final User user = new User("hello");
    User user2 = new User("zxhtom");
    user = user2;
    System.out.println(user.getName());
}
Copy the code
  • If the program were written this way, an error would be reported at compile time. Because the final modified user points to an address that points to user2. This violates Java design principles

conclusion

  • Learning must be grounded. Don’t be too literal, though. But in what context do we need to distinguish each feature

It was the middle of the night. A great bai