Why use private static final

Why use private static final

Three modifiers: private, static, final.

private: Indicates that the property or method is private and corresponds to public and protected. public means public, other classes or subclasses of this class also allow access to properties modified by public, etc.; protected means protected, other classes cannot access properties modified by protected or other things but allow subclasses of this class to access; private Represents private, does not allow other classes except this class to access, including subclasses are not allowed to access properties modified by private or others.

static: Static, static modified things (code blocks, attributes, etc.) do not belong to any object, belong to a certain class, and are public to the object. Static blocks are allowed anywhere, but not inside methods.

final: Final, that is, modification is not allowed. Once assigned, things modified by final are no longer allowed to be modified. When final modifies a reference variable, the reference cannot change its reference address, but the attributes of the reference are still allowed to be modified. When a method is modified as final, the modified method cannot be overridden. Final modified methods are faster than non-final methods because they are statically bound at compile time and do not need to be dynamically bound at runtime. A class modified by final is called a final class. Usually, the functions of the class modified by final are complete, because inheritance (String, Integer, etc.) is not allowed.

When final modifies a variable, the variable will be initialized when the class is loaded, and will be created and loaded due to the creation of the object.

When a static variable is modified, the variable will only be initialized once and will not be reinitialized thereafter.

It can be seen that final and static are different. If there are two properties modified by final and static in a class, the properties modified by static will only be initialized once when the object is created, while the properties modified by final will be initialized as the object is created. Create initialization.

Look at the code below:

public class Atest {
    
    
    private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    private final String fs=sdf.format(new Date());
    private static String ss=sdf.format(new Date());
    public static void main(String[] args) {
    
    
        Atest aa=new Atest();
        try {
    
    
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        Atest bb=new Atest();
        System.out.println("aa.fs="+aa.fs);
        System.out.println("aa.ss="+aa.ss);
        System.out.println("bb.fs="+bb.fs);
        System.out.println("bb.ss="+bb.ss);
    }
}

The result of the operation is as follows:

img

It can be seen that the value of the attribute fs modified by final changes because a new object is created after the thread sleeps for one second. The attribute fs modified by final alone belongs to the object, aa and bb are two objects, and the variable modified by static belongs to the class so no change.

Take a chestnut, such as log

Usually when we use Log to print logs, we will write like this:

private static final Log log=LogFactory.getLog(Test.class);

Here we declare the logger as private, static, and final. What is the reason? First of all, the logger should be something inside a class, and its subclasses or other classes are not allowed to use it, so it is modified as private by private. Secondly, only one logger is needed for all objects of this class, that is, all instances of this class, so use static modification. And the logger cannot be replaced or modified, so use final to modify it.

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/130632526