Static and final and static final

As we all know, static (instantiation, global) is a static modification keyword: it can modify variables, program blocks, methods and classes.

1. Modify variables. (the variable has nothing to do with the object. All references to the variable point to the same address)

The attribute is loaded with the loading of the class. It is a class variable. It is loaded earlier than the object and can be loaded without new

All objects of the class in which the class variable is located share this attribute and are stored in the static field

It is learned that if static modifies a variable, the JVM will allocate it on the memory heap. The variable has nothing to do with the object, and all references to the variable point to the same address.

Therefore, when we use this variable, we directly indicate the static variable of the class. Of course, the modifier must be public

1 public class StaticBean {
2     public static String A = "A";
3 }

Mode of use

1 public static void main(String[] args) throws Exception{
2         System.out.println(StaticBean.A);
3     }

2. Decorate the program block and guess what the output result is?. (for system initialization)

1 public class BaseTest {
 3     static{
 4         System.out.println("B");
 5     }
 7     public static void main(String[] args) throws Exception{ 8  System.out.println("A"); 9  } 10 }

Conclusion: the JVM will preferentially load the code in the static block, so it will preferentially output the B, static modified code block, which is mainly used for system initialization.


3. Modification method: (class method, loading earlier than object, no need for new)

Methods are loaded with the loading of classes. They are class methods. They are loaded earlier than objects and do not need new

All objects of the class in which the class method is located share this method

Class methods can only call static properties and methods, not non static properties and methods
Conversely, non static methods can call static properties and methods

When calling static methods externally, you can use "class name. Method name" or "object name. Method name". The instance method has only the latter method. In other words, calling a static method does not need to create an object. When a static method accesses the members of this class, it is only allowed to access static members, not instance member variables and instance methods.

public class StaticBean {
    public static String A = "A";
    public String D;
    public static void getMessage(){

Which sentence of the above code is wrong is obvious.


4. Decoration. As we know, static modifier is generally used to modify variables, program blocks and methods, but when to use static to modify classes? (only inner classes can be modified.)

Ordinary classes are not allowed to be declared static. Only internal classes can be declared static

The internal class modified by static can be directly used as a common class without instantiating an external class

Internal class. If the external class is declared as static, the program will not be compiled.

The characteristics of internal class are as follows:

That's very simple. When will static inner classes be used? Let's look at the following example

1 public class Outer {
 2     private int i = 0;
 4     public Outer() {
 5         i++;
 6         System.out.println("=====init Outer "+i+"===="); 7 8  } 9 10 public static Outer getInstance(){ 11 return Inner.INSTANCE; 12  } 13 //Static inner class 14 public static class Inner{ 15 private static final Outer INSTANCE = new Outer(); 16  } 17 }


1 public class BaseTest {
2     public static void main(String[] args) throws Exception{
3         for(int i = 0; i < 1000;i++) {
4             Outer.getInstance();
5  } 6  } 7 }

Output result:

=====init Outer 1====

We conclude:

Since INSTANCE is a constant, it can only be assigned once; It is still static, so it is loaded together with the internal classes. This is also an implementation of the single INSTANCE lazy mode, and ensures thread safety at the same time.


The final keyword can be used to modify classes, methods, and variables

1. Decoration

Indicates that this class is not allowed to be inherited, and the member methods in the final class will be implicitly specified as final methods.

public final class FinalBean {

    public  void test(){


2. Modification method

Indicates that the method cannot be overridden, and the private method of a class will be implicitly specified as the final method.

The following example reports an error in the test method of SunFinalBean.

public class FinalBean {

    public final void test(){


    public class SunFinalBean extends FinalBean{
        public void test(){

3. Modification variables

Indicates that the variable must be initialized and the value cannot be changed. If it is a basic type, the value cannot be changed. If it is a reference type, the reference address cannot be changed, but the content of the object pointed to by this reference can still be changed.

Guess what, the following sentence won't work with the compiler.

public class FinalBean {
    private final int i = 0;
    private final int j;
    private final String name = "";

    public FinalBean(){ j = 1;"123"); = "123"; } }

This sentence can be understood by remembering the principle of final. Then why this name. concat("123"); No error will be reported, because the underlying implementation returns a new String object = "123";


Use it with static final:

The static modified attribute emphasizes that they have only one, and the final modified attribute indicates that it is a constant (it cannot be modified after creation). The attribute modified by static final indicates that once a value is given, it cannot be modified and can be accessed through the class name.

static final can also modify a method to indicate that the method cannot be overridden and can be called without a new object.

Tags: Java

Posted by azazelis on Wed, 13 Apr 2022 22:23:52 +0930