concept
Final has the meaning of "unchanged" and can modify non -abstract class, non -abstract members' methods and variables.
Note:
FINAL cannot be used to modify the structure method, because the concept of "method coverage" only applies to the member method of class, not to the constructive method of the class. Relationship, so it is meaningless to modify the constructor.
The method of modified by Private in the parent class cannot be covered by the method of the subclass, so it can be understood as the Private type method by default.
final class
Define the class as final so that this class cannot be inherited. The specific use scenarios are as follows:
For example: Java.lang.String class in JDK is defined as Final type.
public final class string {...}
final method
In some cases, due to safety reasons, the parent class does not allow subclasses to cover a certain method. At this time, this method can be declared as a final type.
For example: In the java.Lang.object class in JDK, the getClass () method is the final type, and the equals () method is not the final type.
All Object subclasses can cover the EQUALS () method, but cannot cover the getClass () method.
final variable
The variable modified variables indicate the constant where the value is not changed.
For example: two constants are defined in JDK java.lang.integer classes.
Public static final into min_value = 0x80000000;
Public static final int max_value = 0x7ffffffff;
FINAL variable has the following characteristics:
Public Class Demo {Public Static Final INT MAX_VALUE = 23; // Static constant Public Static Final Int MIN_Value = 10; // Static constant PRIVATE FINAL DATE BITHDAY = NEW DATE (); // Member constant}
Static constants are generally named after capital letters, and the words are separated by "_" symbols.
Public Class Finaltest {Final Int A = 1; // Member constant defaults to initialize the static final into; // Static constant default Final Int C; // Member constants Test () {C = 3; // The members' constant constant is not initialized by default, and it can initialize the constructor} Static {d = 4; // Static constant is not initialized by default, and initialize it in static code blocks}}}}
Final variables can only be given one value. If the reference type variable is modified with final, the variable can always be referenced by one object, but it can change the content of the object.
public class finalTest {string str = "" "" "" "" "" "" "" "" "" "" "" "; LTEST = New Finaltest (); Finaltest.str = "xixihaha"; finalTest.print ();}}
Summarize:
In the actual program, the constant is defined through the final modifier. The purpose is:
The above is all the contents of this article. I hope it will be helpful to everyone's learning.