Java-based final and static keywords, javafinalstatic

Source: Internet
Author: User

Java-based final and static keywords, javafinalstatic
I. final
According to the context of the program, it can modify non-abstract classes, non-abstract class member methods and variables. 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. 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, variables are also called final blank, no matter what the situation is, the compiler ensures that the blank final must be initialized before use. 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. 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.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 the class is loaded, the Java Virtual Machine can locate 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. The static variable can be modified by private, indicating that the variable can be used in the static code block of the class, other static member methods of the class, or non-static member methods, however, it is important that you cannot directly reference other classes by class names. In fact, you need to understand that private is the access permission limitation, and static means that it can be used without instantiation, so that it is much 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 names use static code blocks to represent static code blocks. When a Java Virtual Machine (JVM) loads a class, the code block is executed. 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 (that is, the static member variables and member methods are not included). You can only access static member variables and member methods of the class. Because instance members are associated with specific objects! You need to understand the truth, not the 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 a JVM loads a class, it executes these static code blocks. If there are multiple static code blocks, the JVM executes them in sequence according to the sequence they appear in the class, and each code block is executed only once. 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. Pay special attention to one problem: for instance constants modified by static and final, the instance itself cannot be changed, but for instance variables of some container types (such as ArrayList and HashMap, the container variable itself cannot be changed, but the objects stored in the container can be modified, which is used in programming. Objects "loaded" in the container type variables modified by static final can be changed.

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.