SoFunction
Updated on 2025-03-08

Detailed explanation of the difference between static and final and static final

As we all know, static is a static modification keyword: variables, program blocks, methods, and classes can be modified.

1. Modify variables.

Know that if static modifies a variable, the JVM will allocate it on the memory heap, which is independent of the object, and all references to the variable point to the same address.

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

public class StaticBean {
   public static String A = "A";
 }

How to use

 public static void main(String[] args) throws Exception{
    ();
  }

2. Modify the program block and guess what the output result is?

public class BaseTest {
  
  static{
    ("B");
  }

  public static void main(String[] args) throws Exception{
    ("A");
  }
}

Conclusion: JVM will prioritize the loading of code in static blocks, so it will prioritize the output of B and static modifier code blocks, which are mainly used for system initialization.

B
A

3. Modification method: When calling static methods externally, you can use the "class name. method name" method or the "object name. method name". The example method is only the latter method. In other words, calling static methods does not require creating objects. When static methods access members of this class, they only allow access to static members, and do not allow access to instance member variables and instance methods.

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

Which sentence in the above code is wrong, it is obvious.

(D);

4. Modification category. In our familiar knowledge, the static modifier is generally used to modify variables, program blocks, and methods, but when should static be used to modify classes?

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

The internal class features are as follows:

  1. 1. Do not hold references to external classes (normal internal classes hold)
  2. 2. You can create an instance directly without creating an external class first (needed by ordinary internal classes)
  3. 3. There can be static member variables, methods (non-normal internal classes do not work) and non-static member variables, methods.
  4. 4. You can only directly access static members of the external class, but not directly access non-static members of the external class (ordinary inner classes can). You need to pass in external class references to access.
  5. 5. When a class is loaded, its inner class will not be loaded at the same time. A class is loaded, which occurs when and only if one of its static members (static domains, constructors, static methods, etc.) is called

That's very simple to introduce, when will static inner classes be used? Let's take a look at the following example

public class Outer {
  private int i = 0;

  public Outer() {
    i++;
    ("=====init Outer "+i+"====");

  }

  public static Outer getInstance(){
    return ;
  }
  // Static internal class  public static class Inner{
    private static final Outer INSTANCE = new Outer();
  }
}

Caller

public class BaseTest {
  public static void main(String[] args) throws Exception{
    for(int i = 0; i < 1000;i++) {
      ();
    }
  }
}

Output result:

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

Let's summarize:

Since INSTANCE is a constant, it can only be assigned once; it is still static, so as to load the inner class together, this is also an implementation of the singleton lazy mode, while ensuring thread safety.

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

1. Modification category

It means that the 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 a final method.

The following example SunFinalBean test method reports an error.

public class FinalBean {

  public final void test(){

  }

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

3. Modify 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 in the object pointed to by this reference can still be changed.

Guess, the following sentence cannot be matched by compiler compilation.

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

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

This sentence, remember the principle of final to understand, then why ("123"); will not report an error, because the underlying implementation returns a new String object

 = "123";

Then use static final together:

The static modified attributes emphasize that there is only one of them, and the final modified attributes indicate that they are a constant (cannot be modified after creation). The static final modified attribute indicates that once a value is given, it cannot be modified and can be accessed through the class name.

static final can also modify the method, indicating that the method cannot be rewritten and can be called without a new object.

The above is a detailed explanation and integration of the differences between static and final and static final introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!