“This is the 24th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!

May you keep your love and go to the mountains and seas in the coming days!

Note: As for the final keyword, it is also a keyword we often use. It can be modified on a class, or on a variable, or on a method to define some of its immutability!

The String class, for example, is final, and its character array is final. But have you really understood some details of the final?

Start with this article and take you deep into the details of final!

An in-depth look at πŸ‘‹ Final

🀚 Take a closer look at modifying variables

The final keyword modifiers variables. Yesterday we learned about member variables. Today we will learn about local variables. Specific let’s have a look!

πŸ–οΈ decorates local variables

Local variables are declared in a method, constructor, or statement block;

Modify the characteristics of local variables:

  • forfinalWhen modifying a local variable, if it has already been initialized, it cannot be changed again later.
  • iffinalModifier a local variable that is not initialized can be assigned, ** “if and only once” ** is assigned, and an error occurs once it is assigned again.

We still continue to meet code, only a look, to understand more!

For variables, there are two types of data type modifications: reference types and primitive data types. The next two types will be described in detail.

“Basic data variables:”

Let’s see if final is true when it modifies basic datatype variables.

package com.nz.test; Public class LocalVariableFinalTest {public void localVariableTest(){public void localVariableTest(){public void localVariableTest() final int localVariable; // We can see that there is no compile-time error if we do not initialize it. // But if you reference it without initialization, it will report an uninitialized error. System.out.println("localVariable = " + localVariable); LocalVariable = 1; LocalVariable = 2; localVariable = 2; }}Copy the code

We can see two problems if we define a final local variable:

  1. We don’t have to do an initial assignment and we don’t get an error, but you can’t reference it without doing an initial assignment, because it doesn’t have a default value, so you have to do an initial assignment before you can reference it.
  2. In thefinalModifier local variables, ** “if and only once” ** assignment, once the assignment after the next assignment, this is the same error as the previous modifier member variables.

“Reference type variable:”

In the primitive data type example above, we found that once the final modified primitive variable has been initialized, the next assignment will fail. This means that variables of primitive data types cannot be modified. What about reference type variables? Can it change? Let’s take a look!

package com.nz.test; Public class LocalVariableFinalTest {// Public class LocalVariableFinalTest {// Public class LocalVariableFinalTest {// Public class LocalVariableFinalTest { Public static void main(String[] args) {// define a BasketballPlayer -- lebron James final BasketballPlayer James = new BasketballPlayer(36, "lebron James "); // Then we can do a series of operations on it! // 1. Reinitialize assignment! // To compile, comment it out! // James = new BasketballPlayer(36, "Old emperor!" ); // 2. Reassign james.setName(" Sunset red team captain! ") to James. ); // You can see that there is no error! Let's try to print it out and see if it's lebron James or The Crimson Captain? System.out.println("james = " + james.getName()); }} class BasketballPlayer {// private int age; private String name; Public BasketballPlayer(int age, String name) {this.age = age; this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}Copy the code

Results obtained:

James = Captain of the Sunset Red Team!Copy the code

We can see two problems:

  1. If we want to get rightfinalReinitializing an assignment to a reference to a type variable will also give you an error because it is reinitializedfinalThe object to which a modified reference variable refers is immutable once it is initialized. Your new operation assigns a new address, which does not work.
  2. When we want to modify the parameters in the reference type, we apply thefinalModify the reference type variable James in the name attribute to sunset red team captain, can be modified successfully drop! Although the address referenced above cannot be changed, the attributes of the object under that address can be changed.

Summary:

Once initialized, a reference variable modified by final refers to an immutable object, but its properties are mutable.

🌸 summary

Believe everybody the reader that a keyword for final had certain understanding, actually additional expand own aspect of knowledge is quite necessary, otherwise people ask you, would you speechless, and once you after in-depth knowledge, every day you in the future articles will gush, shinning!!!!! Right? We still have a handful of things to explore and explore! Let’s continue to look forward to the final content of the next chapter. Welcome to the next chapter!

Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you talent leaders ** “comments” criticism and correction! Of course, if this article is sure to help you a little bit, please kindly and lovely talent leaders give a “like, favorites” next bar, “one key three even” **, thank you very much!

Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!

Thank you all for seeing this! May you live up to your youth and have no regrets!