Java Keywords Final use method detailed _java

Source: Internet
Author: User
Tags constant java keywords

What it says is "this part is not modifiable". There are two reasons not to be changed: efficiency, design. There are three things to use in final: data, methods, classes.

First, final data

Sometimes the constant invariant of data is very useful, and it can reduce the load of the system when it is running. For these constant invariant data I can be called "constants". "Constants" are mainly used with the following two places:
1, compile-time constants, can never be changed.
2, runtime initialization, we hope it will not be changed.
For a compile-time constant, it completes initialization in the class-loading process, so when the class load is complete it is immutable, and the compilation period can be used to take it to any calculation that uses it, meaning that it can be executed at compile time. Of course, for compile-time constants, only basic types can be used and must be initialized at the time of definition.
There are variables that we want to behave differently depending on the object, but at the same time do not want it to be changed, at which point we can use the Run-time constants. For Run-time constants, it is both a basic data type and a reference data type. The basic data type is immutable, and the reference data type is immutable in its reference, and the object content specified by the reference is mutable.

Copy Code code as follows:

public class Person {
private String name;

Person (String name) {
THIS.name = name;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}
}

public class Finaltest {
Private final String final_01 = "Chenssy"; Compile-time constants that must be initialized and cannot be changed
Private final String final_02; Constructor constants that are initialized when an object is instantiated.

private static Random Random = new Random ();
Private final int final_03 = Random.nextint (50); Initialize using random numbers

Reference
Public final person final_04 = new Person ("Chen_ssy"); Final point to reference data type

Finaltest (String final_02) {
this.final_02 = final_02;
}

Public String toString () {
return "final_01 =" + final_01 + "final_02 =" + final_02 + "final_03 =" + final_03 +
"final_04 =" + Final_04.getname ();
}

public static void Main (string[] args) {
System.out.println ("------------Create object------------for the first time");
Finaltest final1 = new Finaltest ("cm");
System.out.println (FINAL1);
System.out.println ("------------Create object------------for the second time");
Finaltest final2 = new Finaltest ("ZJ");
System.out.println (FINAL2);
SYSTEM.OUT.PRINTLN ("------------Modify the referenced object--------------");
Final2.final_04.setName ("Chenssy");
System.out.println (FINAL2);
}
}

------------------
Output:
------------create the object for the first time------------
final_01 = Chenssy final_02 = cm Final_03 = Final_04 = Chen_ssy
------------create the object the second time------------
final_01 = Chenssy final_02 = ZJ final_03 = + final_04 = Chen_ssy
------------Modify a Reference object--------------
final_01 = Chenssy final_02 = ZJ final_03 = + final_04 = Chenssy

Here is only one thing: do not think that some data is final can know its value at compile time, through final_03 we know, here is to use the random number of its initialization, he will be in the runtime to know its value.

Second, final method

All final annotated methods cannot be inherited or changed, so the first reason used for the final method is method locking to prevent any subclasses from modifying it. As for the second reason is the question of efficiency, I do not understand this efficiency problem very clearly, excerpt this paragraph on the Internet: in the early implementation of Java, if a method is indicated as final, it is agreed that the compiler will convert all calls to the method into an inline call. When the compiler discovers a final method invocation command, the it will, based on its own discretion, skip the normal invocation of the Insert program code and execute the method invocation mechanism (push the parameters into the stack, jump to the method code, and then jump back and clear the parameters in the stack, processing the return values), and replaces the method call with a copy of the actual code in the method body. This eliminates the overhead of the method invocation. Of course, if a method is large, your program code expands, and you may not see the performance improvements that are being embedded, because the resulting performance is reduced by the amount of time spent in the method.
To this paragraph understanding I am not very understand to copy, that Java Cow person can explain under!!
The final method of a parent class cannot be overridden by a quilt class, which means that subclasses cannot exist in exactly the same way as the parent class.

Copy Code code as follows:

public class Custom extends person{
public void Method1 () {
System.out.println ("person ' s method1 ...");
}

Cannot override the final methods from: Subclasses cannot overwrite the final method of the parent class
public void Method2 () {
System.out.println ("person ' s method2 ...");
//    }
}

Third, Final class

If a class is final modified to indicate that the class is the end class, it does not want and does not allow others to inherit it. In the design of a security or other reason, we do not allow the class to have any changes, and do not want it to have subclasses, this time you can use final to modify the class.
For the final-decorated class, its member variables can be final or not final. If the definition is final, then the final data rule is also appropriate for it. And its method will automatically add final, because the final class cannot be inherited, so this is the default.

Iv. Final Parameters

In practice, we can modify the member variable, the member method, the class, and the parameters, and if a parameter is final decorated, then the parameter can not be changed.
If we modify this parameter in the method, the compiler prompts you: The final local variable I cannot be assigned. It must is blank and not using a compound assignment.

Copy Code code as follows:

public class Custom {
public void test (final int i) {
i++; ---Final argument cannot be changed
System.out.println (i);
}

public void Test (final person p) {
p = new person (); --final parameter is not variable
P.setname ("Chenssy");
}
}

The same final modifier parameter is useful in the inner class, and in the anonymous inner class, in order to maintain the consistency of the parameters, the formal parameter must be final if the parameter of the method is used inside the inner class.

Vi. Final and Static

Final and static use can have magical chemical reactions, they can be used to modify the member variables, but also to modify the member method.
for a member variable, the variable cannot be changed once it is assigned, which we call "global constant." can be accessed directly through the class name. The
is not inheritable and changed for member methods. can be accessed directly through the class name.  

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.