“This is the 22nd 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!

Basic use of 🙆 final

First of all, you must know how to use final in order to further understand it and analyze it.

The use of the final keyword has three characteristics:

  1. If a class is declared asfinal, meaning that it cannot be derived from new subclasses, that is, cannot be inherited.
  2. Declare the variable asfinal, can guarantee that they will not be changed in use. However, is declared asfinalThe variable must be given an initial value when declared, and can only be read in future references.
  3. Be declaredfinalThe method can also be used only and cannot be overridden in subclasses.

🙋 modifies variables

Declaring variables final ensures that they will not be changed in use. However, variables that are declared final must be given initial values at declaration time and can only be read in future references.

package com.nz.test; /** * Test final modifier variables! */ public class VariableFinalTest {// If a variable is modified by final, it must be initialized. private final int variable; public static void main(String[] args) { final int num = 2; // Base variable modified by final, indicating that it cannot be modified. num = 3; final Object obj = new Object(); // Reference variables modified with final, also indicating that they cannot be modified. obj = new Object(); }}Copy the code

We can see that in the uninitialized variable, after initialization to modify the value of the variable, re reference to the new reference address, are red during compilation, will report error drops!

🙇 modification method

Methods declared final are also available only and cannot be overridden in subclasses.

You can declare a method final if you think the functionality of the method is well written and no changes are needed in subclasses. Final modified methods are faster than non-final modified methods because they are already statically bound at compile time and do not need to be dynamically bound at run time!

Let’s see if we really can’t rewrite it

package com.nz.test; /** * Test final decorator method! * This class is the parent class. When a subclass inherits this class, SaySomething () public final String saySomething(){return "What are you trying to say, Lakers champion!!" ; } // talkSomething() method public String talkSomething(){return "I want to say something, I want to say something!!" ; }} class extends MethodSun extends MethodFinalTest{ TalkSomething () public String talkSomething(){return "I can only agree with you, old age, quick red!!" ; } public String saySomething(){return "I can only agree with you, lakers champion!!" ; }}Copy the code

As we can see, if we do not have “talkSomething”, it is not modified by final and can be perfectly rewritten. However, if our “saySomething” method is modified by final, it cannot be rewritten.

🌸 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 to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, 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!

\