Depending on the context, Java's keywordfinal also has subtle differences, but usually refers to "this cannot be changed." "There are two reasons not to change: one is efficiency, and the other is design. Because two reasons are very far apart, the
(inkfish original, reprint please indicate the source: http://blog.csdn.net/inkfish/)
In order to seek the performance optimization of Java code, search from the internet to use final keyword compile-time inline optimization method, but really
1. Modifier classWhen a class is decorated with final, it indicates that the class cannot be inherited. In other words, if a class you never let him inherit, you can use final to decorate. Member variables in the final class can be set to final as
Static:1. In a variable, the method is pre-decorated to represent static methods, static variables.2.Static {}This form is a static block of code used to initialize a variable that is also modified by the final static. (Of course, the more common
It is well known that when accessing a variable or method of a class, if the class is not initialized, a class is initialized first.However, when the variable of this class is final, it is not necessarilyTake a look at the following examplePackage
Final Keyword:Final can modify classes, methods, variablesThe final decorated class cannot be inheritedThe final modified method cannot be overriddenA final modified variable is a constant that can only be assigned onceInner classes can only access
According to the final keyword conclusion that Haizi blog has been manually verified and considered, Haizi final
Final keywords (modifier constants, variables, methods, classes)1. final Modifier
As we can see from the above, the final modified
Final: Finally, as a modifier
Can be decorated with classes, functions, variables
A class that is final modified cannot be inherited, in order to avoid the quilt-like replication function
The final modified method cannot be
Final ClassFinal class, in fact, it is a class of special requirements: it is required that the class is not allowed to inherit down. Form:Final class class name {the member definition of the class ... Same as the definition of the general Class!
Package com.bawei.multithread;//Note: Template methods we usually use abstract classes or abstract methods! Here we do not use abstract class/abstract method in order to facilitate use in this class Templatethread {//If this method does not want the
Final (final) is a modifier1, final can be modified classes, functions, variables (member variables, local variables)2, the final modified class can not be inherited by other classes3, the final modified method (function) can not be rewritten4, the
(1) Final defines constants in Java that can be used for primitive types or class types, and if they are in a class type, such a type cannot be a parentClass is inherited, that is, it cannot have subclasses under it, and such classes are called
Final
The final decorated class, which indicates that the class cannot be inherited
The final modification method, which indicates that this method cannot be overridden
Final modifier constant (constant capitalization), indicating
Strictly speaking, the final modified variable cannot be changed, and once the initial value is obtained, the value of the final variable cannot be re-assigned, so there is a difference between the final decorated member variable and the decorated
In a programming language, there is a way to tell the compiler that a piece of data is immutable. There are two requirements
1. A compiler constant that never changes2. A value that is initialized at run time, and this value is not changed
In Java
This example describes the final keyword usage in java. Share to everyone for your reference, specific as follows:
Depending on the context, the Java keyword Final also has a subtle distinction, but it usually means "this is immutable." "There are
Java static keywords and Java statics and static methodsThe static modifier can be used with a variable, a method, and represents a "static".
Static variables and static methods can be accessed through the class name and do not need to create an
first, final modification features
Modifier class, the class cannot be inherited (all methods in the class cannot be overridden) modify the variable, the variable becomes a constant, can only be assigned once to modify the method, the method can
1. public static final String string = "Hello world!";Modifies a class member variable, which is assigned a value here, and does not allow subsequent modifications to the value, which is what is commonly said to be constant. Must be initialized when
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.