Summary of static and final usage in Java

Source: Internet
Author: User

First, final variables:

When you define a variable in a class and precede it with the final keyword, it means that once the variable is initialized it is immutable, and the immutable meaning is immutable for the base type, and the reference to the object variable cannot be changed. Its initialization can be in two places, one is its definition, that is, when the final variable is defined directly assigned to it, and the second is in the constructor. These two places can only be selected, either in the definition of the value, or in the constructor to give a value, not both in the definition of the value, but also in the constructor to give another value.

When the function parameter is the final type, you can read it using the parameter, but you cannot change the value of the parameter.

In addition, when an inner class in a method is used with a parameter variable in a method, this parameter must also be declared final to use method

If a class does not allow its subclasses to overwrite a method, you can declare this method as the final method.
There are two reasons for using the final method:
First, lock the method to prevent any inherited classes from modifying its meaning and implementation.
Second, efficient. The compiler goes into an inline mechanism when it encounters a call to the final method, greatly improving execution efficiency. class

The final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. In the design class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, and it is certain that the class will not be extended, then it is designed as the final class.

Second, static

member variables and member methods that are modified by static are independent of any object of the class. That is, it does not depend on class-specific instances and is shared by all instances of the class. As long as this class is loaded, the Java virtual machine can find them within the method area of the runtime data area based on 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 have a private adornment before it, indicating that the variable can be used in the static code block of the class, or in other static member methods of the class (which can also be used in non-static member methods-nonsense), but it is important that the class name cannot be referenced directly in other classes. In fact, you need to understand that private is the access permission limit, static means do not instantiate can be used

A static code block is represented by a statically decorated block of code that executes when the Java Virtual Machine (JVM) loads the class

1.static variables

There are two ways to classify a class member variable statically: A variable that is statically modified, called a static variable or a class variable, and another variable that is not modified by static, called an instance variable. The difference between the two is:
For static variables there is only one copy in memory (memory saving), the JVM allocates only one memory at a time, completes the memory allocation of static variables during the loading of the class, can be accessed directly (conveniently) by the class name, and, of course, is accessible through objects (but this is not recommended). For instance variables, when an instance is not created, the instance variable is allocated one memory, and the instance variable can have multiple copies in memory without compromising (flexibility).

2.static method

Static methods can be called directly from the class name, and any instance can also be called, so a static method cannot use the This and Super keywords, and cannot directly access the instance variables and instance methods of the owning class (that is, member variables and member methods without static). Only static member variables and member methods of the owning class can be accessed. Because the static method is independent of any instance, the static method must be implemented, not abstract.

3.Static code block

A static block of code is also called a code block, which is an independent block of static blocks in a class that is separate from a class member, can have more than one position, it is not in any method body, and the JVM executes these static blocks of code when the class is loaded, and if there are multiple static blocks of code, The JVM executes them sequentially in the order in which they appear in the class, and each code block is executed only once.

4, the static and final pieces are used to indicate what  
Static final is used to modify member variables and member methods, which can be simply understood as " Global Constants "!  
for a variable, it means that once the value is given it cannot be modified and is accessible through the class name.  
for methods that represent non-overwriting and 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: 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.