1. Final variable
Final variable is a constant that cannot be changed once it is initialized.
Class Test1 {
final double PI = 3.14;//The name of the constant is best capitalized public
Test1 () {
PI = 3.14;
}
void Test () {
System.out.println ("PI is:" + pi);
}
public static void Main (string[] args) {
Test1 t = new Test1 ();
T.test ();
}
Output:
(1) Blank final variable
Final variable that are uninitialized at the time of the declaration are called Blank final variable, blank final variable must be initialized in the constructor, or a compilation error will be thrown.
Class Test1 {
final double PI;
Test1 () {
pi = 3.14;//Initialization of
}
void Test () {
System.out.println ("PI is:" + pi) in the constructor;
}
public static void Main (string[] args) {
Test1 t = new Test1 ();
T.test ();
}
Output:
(2) uninitialized static final variable
Static final variable uninitialized in the declaration phase can only be initialized in a static code block
Class Test1 {
static final double PI;
static {
PI = 3.14;
}
void Test () {
System.out.println ("PI is:" + pi);
}
public static void Main (string[] args) {
Test1 t = new Test1 ();
T.test ();
}
Output:
2. Final method
Final method cannot be overwritten. That is, subclasses can invoke the Fianl method of the parent class, but they cannot overwrite it.
Class Test {
static final double PI = 3.14;
final void Test () {
System.out.println ("PI is:" + pi);
}
Class Test1 extends test{public
static void Main (string[] args) {
Test1 t = new Test1 ();
T.test ();
}
Output:
3. Final class
Final CALSS cannot be inherited
Final class Test1 {
static final double PI = 3.14;
final void Test () {
System.out.println ("PI is:" + pi);
}
public static void Main (string[] args) {
Test1 t = new Test1 ();
T.test ();
}
Output:
Why is the
PS:java.lang.String class designed to be final?
First, you need to know the final keyword first.
Final appears to have two points for reasons that don't want to change: design or efficiency. The final-decorated class is not inheritable, so the final-decorated class cannot be tampered with.
with this in mind, let's look at the problem:
(1) from the design,
A, make sure that they don't change semantics in subclasses. The string class is the final class, which means that no one is allowed to define a subclass of string. In other words, if you have a string reference, it must be a string object, not an object of another class.
B, string once created can not be modified, because the Java designer will String to be shared, the following is the source of the comments:
(2) from the efficiency:
A, designed to FINAL,JVM not to the relevant methods in the virtual function table query, The execution efficiency is improved by directly locating the related methods of the String class.
B, the Java designer believes that sharing is more efficient.
In summary, it is necessary to ensure that the object referenced by the Java.lang.String reference must be an object of java.lang.String, not a descendant of it, in order to ensure its efficiency and security.