Summary of static and final usage in Java

Source: Internet
Author: User

I. Final

1. Final variable:

When you define a variable in a class and add the final keyword before it, this variable cannot be changed once initialized, the unchangeable meaning here is that its value is immutable for the basic type, and its reference for the object variable cannot be changed. ItsInitializationIt can be in two places.DefinitionThat is, assign a value to the final variable when defining it.Constructor. You can only choose one of these two places, either give a value at the time of definition, or give a value in the constructor, not both at the time of definition, in the constructor, another value is given.

WhenThe function parameter is final.Type, you can read and use this parameter, but cannot change the value of this parameter.

In additionInternal classWhen using the parameter variable in the method, this parameter must also be declared as final for use.

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 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.

Ii. Static

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 find them in the Method Area of the runtime data Zone according to the class name. 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 static in the class.CodeBlock, or other static member methods of the class (of course, you can also use the -- nonsense in non-static member methods), but it cannot be directly referenced by the class name in other classes, this is important. In fact, you need to understand,Private is the access permission restriction, and static means that you can use it without instantiation.

A static code block is a static code block. When a Java Virtual Machine (JVM) loads a class, the code block is executed.

1. static variable

The class member variables can be classified by static or not. One is a variable modified by static, calledStatic variablesOrClass variable; The other is a variable not modified by static, calledInstance variables. 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 ),Only static member variables and member methods of the class can be accessed.. Because the static method is independent of any instanceThe static method must be implemented.Instead of 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 is expressed by static and final
static final is used to modify member variables and members. method, it can be simply understood as " global constant "!
for a variable, it indicates that once the value is given, it cannot be modified and can be accessed through the class name.
for methods, the method cannot be overwritten and can be accessed directly by 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.