Explain the difference between static and final and static and final in detail

  • 2021-07-16 02:22:06
  • OfStack

As we all know, static is a static decorating keyword: it can decorate variables, blocks, methods and classes.

1. Modify variables.

If static modifies a variable, JVM will allocate it on the memory heap, the variable is independent of 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


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

Usage


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

2. Decorate the block and guess what the output is. .


public class BaseTest {
  
  static{
    System.out.println("B");
  }

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

Conclusion: JVM will give priority to loading the code in static block, so it will give priority to outputting B and static modified code block, which is mainly used for system initialization.

B
A

3. Decorate methods: When calling static methods externally, you can use "class name. method name" or "object name. method name". The instance method only has the latter way. That is to say, static methods can be called without creating objects, and when static methods access members of this class, they only allow access to static members, but not to instance member variables and instance methods.


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

The above code, which sentence is wrong, obviously.


System.out.println(D);

4. Modified classes. As we know, the static modifier 1 is generally used to modify variables, blocks, and methods, but when do you use static to modify classes?

Inner class. If the external class is declared as static, the program will compile without passing.

Internal classes have the following characteristics:

1. Do not hold references to external classes (held by ordinary internal classes) 2. You can create instances directly without creating an external class first (required by ordinary internal classes) 3. You can have static member variables, methods (not normal inner classes) and non-static member variables, methods 4. You can only directly access static members of external classes, not non-static members of external classes (ordinary internal classes can), and you need to pass in external class references to access them 5. When a class is loaded, its inner classes are not loaded at the same time. 1 class is loaded if and only if one of its static members (static fields, constructors, static methods, etc.) is called

Then it is very simple to say, when will static inner classes be used? Let's look at the following example


public class Outer {
  private int i = 0;

  public Outer() {
    i++;
    System.out.println("=====init Outer "+i+"====");

  }

  public static Outer getInstance(){
    return Inner.INSTANCE;
  }
  // Static inner 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++) {
      Outer.getInstance();
    }
  }
}

Output:

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

Let's sum up:

Because INSTANCE is constant, it can only be assigned once; It is still static, so with the inner class 1 loading, this is also an implementation of singleton lazy mode, while ensuring thread safety.

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

1. Modify classes

Indicates that the class is not allowed to inherit, and all member methods in the final class are implicitly specified as final methods.


public final class FinalBean {

  public void test(){

  }
}

2. Modification

Indicates that this method cannot be overridden, and the private method of 1 class is implicitly specified as the final method.

The test method of the following example SunFinalBean 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, and if it is a reference type, the reference address cannot be changed, but the contents of the object pointed to by this reference can still be changed.

Guess, the following sentence doesn't pass the compiler compilation.


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

  public FinalBean(){
    j = 1;
    this.name.concat("123");
    this.name = "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


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

Then static final 1 starts:

static-decorated attributes emphasize that they are only 1, while final-decorated attributes indicate that they are 1 constant (which cannot be modified after creation). static final modifies properties that represent a value of 1, are not modifiable, and can be accessed by class name.

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


Related articles: