In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to use the Java final keyword, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The following is a good talk about the final keyword, and I hope to give more friends some help.
Although inheritance allows us to reuse existing code, sometimes for some reason we do need to limit extensibility, and the final keyword can help us do so.
01, final class
If a class is decorated with the final keyword, it cannot be inherited. If friends observe carefully, Java has a lot of final classes, such as the most common String class.
Public final class String
Implements java.io.Serializable, Comparable, CharSequence
Constable, ConstantDesc {}
Why is the String class designed to be final? There are generally three reasons:
In order to implement the string constant pool
For thread safety
For the immutability of HashCode
For more detailed reasons, you can check out an article I wrote earlier.
To verify that any attempt to inherit from the final class will raise a compilation error, let's take a look at the following example, the Writer class is final.
Public final class Writer {
Private String name
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
}
When you try to inherit it, the compiler prompts you that the Writer class is final and cannot be inherited.
However, just because a class is final does not mean that its objects are immutable.
Writer writer = new Writer ()
Writer.setName (Silent King II)
System.out.println (writer.getName ()); / / Silent King II
The default value for the name field of Writer is null, but it can be changed to Silent King II through the settter method. That is, if a class is only final, then it is not an immutable condition.
If you want to know the whole truth about immutable classes, please check out my previous article to say that I don't understand the immutable class this time. Suddenly found that writing a series of articles is really wonderful, many concepts of relevance are all involved. I really convinced myself.
Designing a class as final has its security considerations, but it should not be intentional, because defining a class as final means that it cannot inherit, and if there are some problems with some methods of this class, we cannot fix it by rewriting it.
02. Final method
Methods modified by final cannot be overridden. If we design a class and think that some methods should not be overridden, we should design it as final.
The Thread class is an example, which itself is not final, which means that we can extend it, but its isAlive () method is final:
Public class Thread implements Runnable {
Public final native boolean isAlive ()
}
It is important to note that this method is a native method that is used to confirm that the thread is active. The local method is determined by the operating system, so it is not easy to rewrite the method.
The Actor class has a final method show ():
Public class Actor {
Public final void show () {
}
}
When we want to override the method, a compilation error occurs:
If some methods in a class are to be called by other methods, you should consider that the method being called is called the final method, otherwise, overriding the method will affect the use of the calling method.
A class is final, and a class is not final, but all its methods are final. Consider, what's the difference between them?
The only thing I can think of is that the former cannot be inherited, that is, methods cannot be overridden, while the latter can be inherited and then append some non-final methods. Is everything all right? Look, I'm smart.
03, final variable
Variables modified by final cannot be reassigned. In other words, once the final variable is initialized, it cannot be changed. I have been asked by a friend before, what is effective final and what is final, which I have explained in my previous article, so I will post the address again:
Http://www.itwanger.com/java/2020/02/14/java-final-effectively.html
1) basic data types modified by final
To declare a variable of type int decorated by final:
Final int age = 18
Try to change it to 30, and the compiler gets angry:
2) reference types of final decorations
Now there is a normal class Pig that has a field name:
Public class Pig {
Private String name
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
}
Declare a final-decorated Pig object in the test class:
Final Pig pig = new Pig ()
If you try to reassign pig, the compiler will also be angry:
However, we can still modify the field value of Pig:
Final Pig pig = new Pig ()
Pig.setName ("Maverick")
System.out.println (pig.getName ()); / / maverick
3) final-decorated fields
Final-decorated fields can be divided into two types, one is static, and the other is without static, like the following:
Public class Pig {
Private final int age = 1
Public static final double PRICE = 36.5,
}
Non-static final fields must have a default value, otherwise the compiler will remind you that they are not initialized:
The final field of static is also called a constant, and its name should be uppercase and can be initialized on declaration or via static [code block initialization] ().
4) parameters of final modification
The final keyword can also modify the parameter, which means that the parameter cannot be modified in the method body:
Public class ArgFinalTest {
Public void arg (final int age) {
}
Public void arg1 (final String name) {
}
}
If you try to modify it, the compiler prompts the following error:
After reading the above, have you mastered how to use the Java final keyword? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.