“This is the 21st day of my participation in the Gwen Challenge in November. 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!

Final first impression

I believe that your first impression of final will definitely come from the interview final, Finally and Finalize. What is the difference between final, Finally and Finalize?

Final: The modifier (keyword) can be used in three ways: 1. If a class is declared final, it means that it cannot be subclassed. Therefore, it is the opposite of abstract. 2. Declaring variables as final ensures that they will not be changed during use. Variables declared as final must be given initial values at the time of declaration, and can only be read and cannot be modified in future references. 3. Methods declared final are also available only and cannot be overridden in subclasses. Finally: Usually placed ina try.. catch.. In exception handling, this means that the code will execute whether the program executes normally or if an exception occurs. It is common to put the code that releases the resource ina finally block. Finalize: is a method defined in the Object class. Java allows you to use finalize() to do the necessary cleanup before the garbage collector removes objects from memory. This method is called by the garbage collector when the object is destroyed.Copy the code

Right, not wrong, I am also reciting the eight-part essay, will discover the knowledge point that will not know specifically before. But from today, I will bring you a new understanding of the keyword final!! Let’s GO~

Basic use of final

Today we will first explain how to use final, for its definition you should more or less understand, or look at the answer above.

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.

decorator

If a class is declared final, it cannot be subclassed, that is, cannot be inherited. In Java, you can also see that many classes are final, such as String, Interger, and other wrapper classes.

Public final Class implements Java.io.Serializable, Comparable<String>, CharSequence {... } public final class implements Number implements Comparable<Integer> {... }Copy the code

So let’s see if it’s true that we can’t inherit?

package com.nz.test; /** * test to see if classes really cannot be decorated! Public class ClassFinalTest {} public class ClassFinalTest {} public class ClassFinalTest {} public class ClassFinalTest {} Class AthleticsMan extends SportMan{}Copy the code

As you can see from the diagram, the parent class is modified with final at compile time, and an error is reported when a subclass inherits the parent class.

🌸 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!