Java keywords final and static Use Summary keywords

Source: Internet
Author: User
Tags java keywords

I. Final
According to the context of the program, the Java keyword final has the meaning of "This cannot be changed" or "final state". It can modify non-abstract classes, non-abstract class member methods, and variables. You may need to block changes in two ways: design or efficiency.
The final class cannot be inherited and has no subclass. The methods in the final class are final by default.
The final method cannot be overwritten by the quilt class, but can be inherited.
Final member variables indicate constants and can only be assigned once. The values do not change after the values are assigned.
Final cannot be used to modify the constructor.
Note: The Private member method of the parent class cannot be overwritten by the quilt class method. Therefore, the private type method is of the final type by default.

1. Final class
The final class cannot be inherited. Therefore, the member methods of the final class have no chance to be overwritten. By default, they are all final. When designing a class, if the class does not need a subclass, the implementation details of the class cannot be changed, and it is sure that the class will not be loaded and extended, it is designed as a final class.
2. Final Method
If a class does not allow its subclass to overwrite a method, you can declare this method as a final method.
There are two reasons for using the final method:
First, lock the method to prevent any inheritance class from modifying its meaning and implementation.
Second, efficient. The compiler transfers the final method to the embedded mechanism to greatly improve the execution efficiency.
For example:

Java code

Public class test1 {<br/> Public static void main (string [] ARGs) {<br/> // todo automatically generates method stubs <br/>}< br/> Public void F1 () {<br/> system. out. println ("f1"); </P> <p >}< br/> // method that cannot be overwritten by the quilt class <br/> Public final void F2 () {<br/> system. out. println ("F2"); <br/>}< br/> Public void F3 () {<br/> system. out. println ("F3"); <br/>}< br/> private void F4 () {<br/> system. out. println ("F4"); <br/>}< br/> pub LIC class Test2 extends test1 {<br/> Public void F1 () {<br/> system. Out. println ("test1 parent class method F1 is overwritten! "); <Br/>}< br/> Public static void main (string [] ARGs) {<br/> Test2 T = new Test2 (); <br/> T. f1 (); </P> <p> T. f2 (); <br/> // call the final method inherited from the parent class <br/> T. f3 (); <br/> // call the method inherited from the parent class <br/> // T. f4 (); <br/> // The call fails and cannot be obtained from the parent class <br/>}< br/>}

 

3. Final variable (constant)

The final modified member variable is used to represent constants. Once given, the value cannot be changed!
Three final-modified variables are available: static variables, instance variables, and local variables, representing three types of constants respectively.
As shown in the following example, the final variable value cannot be changed once it is given.
In addition, when final variables are defined, they can be declared first without initial values. In this case, the variables are also called final blank. In any case, the compiler ensures that the blank final must be first used before use.
Start-up. However, the final blank space provides greater flexibility in the use of the final keyword final. For this reason, the final data members in a class can be implemented based on objects,
But there are features that keep them unchanged.

Java code

Package COM. ilibaba. test; <br/> public class test3 {<br/> private final string S = "final instance variable s"; <br/> private final int A = 100; <br/> Public final int B = 90; <br/> Public static final int c = 80; <br/> Private Static final int d = 70; <br/> Public final int e; <br/> // The final is blank. The initial value must be assigned when the object is initialized. <br/> Public test3 (int x) {<br/> E = X; <br/>}< br/>/** <br/> * @ Param ARGs <br/> * <br/> */<br/> Public static void main (string [] ARGs) {<br/> test3 T = new test3 (2); <br/> // T. A = 101; <br/> // error. The final variable value cannot be changed once given. <br/> // T. B = 91; <br/> // error. The final variable value cannot be changed once given. <br/> // T. C = 81; <br/> // error. The final variable value cannot be changed once given. <br/> // T. D = 71; <br/> // error. The final variable value cannot be changed once given. <br/> system. out. println (T. a); <br/> system. out. println (T. b); <br/> system. out. println (T. c); <br/> // It is not recommended to access static fields using objects. <br/> system. out. println (T. d); <br/> // It is not recommended to access static fields using objects. <br/> system. out. println (test3.c); <br/> system. out. println (test3.d); <br/> // system. out. println (test3.e); <br/> // error, because E is a final blank and varies according to different object values. <br/> system. out. println (T. e); <br/> test3 T1 = new test3 (3); <br/> system. out. println (t1.e); <br/> // The final blank variable e varies depending on the object <br/>}< br/> private void test () {<br/> system. out. println (New test3 (1 ). a); <br/> system. out. println (test3.c); <br/> system. out. println (test3.d); <br/>}< br/> Public void Test2 () {<br/> final int A; <br/> // final blank, assign a value only when necessary <br/> final int B = 4; <br/> // local constant -- when final is used for local variables <br/> final int C; <br/> // final blank. No value is assigned. <br/> A = 3; <br/> // A = 4; error. The value has been assigned. <br/> // B = 2; error. The value has been assigned. <br/>}< br/>}

4. Final Parameters
When the function parameter is of the final type, you can read and use this parameter, but you cannot change the value of this parameter.

Java code

Package COM. ilibaba. test; <br/> public class test4 {<br/> Public static void main (string [] ARGs) {<br/> New test4 (). f1 (2); <br/>}< br/> Public void F1 (final int I) {<br/> // I ++; <br/> // I is of the final type and cannot be changed. <br/> system. out. print (I); <br/>}< br/>}

Ii. Static
Static indicates the meaning of "global" or "static". It is used to modify member variables and member methods. It can also form static code blocks, but Java does not have the concept of global variables.
 
The static modified member variables and member methods are independent of any object in the class. That is to say, it is shared by all instances of the class and does not depend on a specific instance of the class. As long as this class is loaded, the Java Virtual Machine
You can find the class names in the Method Area of the runtime data zone. Therefore, a static object can be accessed before any of its objects are created without referencing any objects.
Static member variables and member Methods Modified with public are essentially global variables and global methods. When an object of its class is declared, a copy of static variables is not generated, instead, all instances of the class share the same static variable.

The static variable can be modified by private, indicating that the variable can be used in the static code block of the class or other static member methods of the class (of course, it can also be used in non-static member methods ).
-- Nonsense), but it is important that you cannot directly reference it in other classes by class names. In fact, you need to understand that private is the access permission limitation, and static means that it is not instantiated.
This makes it easier to understand. The effect of adding other access key words before static is also similar.
Static modified member variables and member methods are usually called static variables and static methods. They can be accessed directly by class names. The access syntax is as follows:
Class Name. Static Method Name (parameter list ...)
Class Name. static variable name
A static code block is a static code block. When a Java Virtual Machine (JVM) loads a class, the code block is executed (very useful ).
1. Static variables
Class member variables can be classified by static or not. One is static modified variables, such as static variables or class variables; the other is a variable that is not modified by static, called an instance variable. The difference between the two is:
For static variables that only have one copy (memory saving) in the memory, JVM only allocates the memory for the static one time, and completes the memory allocation of the static variables during the loading process, you can directly access the available class name (convenient). Of course, you can also access it through an object (but this is not recommended ).
If an instance variable is not created, the instance variable will be allocated with memory once. The instance variables can be copied multiple times in the memory without affecting each other (flexible ).

2. Static Method

Static methods can be called directly through the class name, and can be called by any instance. Therefore, this and super keywords cannot be used in static methods, you cannot directly access the instance variables and instance methods of the class (
Is without static member variables and member methods), only the static member variables and member methods of the class can be accessed. Because instance members are associated with specific objects! This requires understanding and understanding
It's not a memory !!!
Because the static method is independent of any instance, the static method must be implemented rather than abstract.

3. Static code block

A static code block is also called a static code block. It is a static statement Block Independent of class members in a class. It can have multiple static code blocks and can be placed anywhere. It is not in any method body, when the JVM loads a class
If there are multiple static code blocks, JVM will execute them in sequence according to the order they appear in the class, and each code block will be executed only once. For example:

Java code

Package COM. ilibaba. test; <br/> public class test5 {<br/> Private Static int A; <br/> private int B; <br/> static {<br/> test5.a = 3; <br/> system. out. println (a); <br/> test5 T = new test5 (); <br/> T. F (); <br/> T. B = 1000; <br/> system. out. println (T. b); <br/>}< br/> static {<br/> test5.a = 4; <br/> system. out. println (a); <br/>}< br/> Public static void main (string [] ARGs) {<br/> // todo automatically generates method stubs <br/>}< br/> static {<br/> test5.a = 5; <br/> system. out. println (a); <br/>}< br/> Public void F () {<br/> system. out. println ("hhahhahah"); <br/>}< br/>}
Running result:
3
Hhahhahah
1000
4
5
Static code blocks can be used to assign values to some static variables. In the end, these examples all use a static main method, so that the JVM can directly call the main method without creating an instance.
4. What does static and final use to represent?
Static final is used to modify member variables and member methods. It can be simply understood as a "global constant "!
For a variable, it means that once the value is given, it cannot be modified and can be accessed through the class name.
For methods, it means they cannot be overwritten and can be accessed directly by class names.

Related Article

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.