Java foundation — final and static keywords

Posted on

1. Final keyword

The final keyword stands for “final”, “immutable”

1.1 modifying variables

  1. Member variable – represents that the current variable is a constant stored in the method area.Must be explicitly initialized, that is, it can be initialized at the time of definition or in the constructor!
  2. Local variables – variables are immutable throughout the scope!
    public void function(){
     final int age = 0; //This local variable modified by final only needs to be initialized before use. It does not need to be initialized as explicitly as a member variable
     age ++; //Error report!!! Final type is immutable!
    }

1.2 modification method

The final modifier method means that the method cannot be overridden by subclasses!!! The private method is implicitly specified as final!!!

1.3 modification

The final modifier class represents a class that cannot be inherited!!!

Advantages of final

  • The final keyword improves performance. Both JVM and Java applications cache final variables.
  • Final variables can be shared safely in a multithreaded environment without additional synchronization overhead. For example, the string bottom layer is an array of final type, so the string type is thread safe!
  • Using the final keyword, the JVM optimizes methods, variables, and classes.

2. Static keyword

2.1 static guide package

import static java.lang.Math.*
public class Test{
    public static void main(String[] args){
        int a = abs(-1); //[math.] can be omitted directly
    }
}

2.2 modifying variables

The biggest difference between static variable and instance variable is that static variable belongs to class variable, while ordinary member variable belongs to instance variable,Live and die with examples. After all, class variables are placed in the method area, and instance variables are placed in the heap with objects!

2.3 modification methods

Like variables, static decorated methods belong to classes!

  • Class methods are loaded with the class loading, and each object shares one! The normal method loads with the creation of the object
  • Class methods can only call external class variables, while ordinary methods can be called at will
  • Class method can use limited keywords, such as super and this!! The ordinary method doesn’t matter

2.4 modifying code blocks

When decorating a code block, the rules are similar to the decorating methodWhen the class is loaded, it can only be loaded once. Only static variables can be called, and keywords are limited

Java foundation -- final and static keywordsKey point: when the execution of a piece of code involves the static variables of parent-child classes, static code blocks, member variables and common code blocks, what is their execution sequence?Java foundation -- final and static keywords

Java foundation -- final and static keywordsStatic variables and static code blocks of the parent class

Java foundation -- final and static keywordsStatic variables and static code blocks of subclasses

Java foundation -- final and static keywordsInstance variables and common code blocks of the parent class

Java foundation -- final and static keywordsConstructor of parent class

Java foundation -- final and static keywordsSubclass instance variables and common code blocks

Java foundation -- final and static keywordsConstructor of subclass

2.5 modify inner class

A kind of inner class,The static inner class does not depend on the outer class, but the instantiation of the ordinary inner class must depend on the instantiation of the outer class!!!


Yan Shen: talk about Java inner classJava foundation -- final and static keywords

In Java, aClass is defined in another class or in a methodSuch a class is called an inner class. Generally speaking, there are four kinds of inner classes: member inner class, local inner class, anonymous inner class and static inner class.

  1. Member inner class

    Member inner class is the most common inner class. It is defined as being inside another class, as follows:

    class Circle {
     double radius = 0;
    
     public Circle(double radius) {
         this.radius = radius;
     }
    
     class Draw {     //Member inner class
         public void drawSahpe() {
             System.out.println("drawshape");
         }
     }
    }

    In this way, the class draw looks like a member of the class circle, which is called the outer class.Member inner classes have unconditional access to all member properties and member methods (including private members and static members) of outer classes.Although the member inner class can access the members of the outer class unconditionally, the outer class does not want to access the members of the member inner class at will. In an external class, if you want to access members of an internal class,You must first create an object of a member inner classAnd then access it through a reference to this object

    Java foundation -- final and static keywordsThat is to say, if you want to create an object of a member’s inner class, you must have an object of an outer class.

  2. Local inner class

    Local inner class is a class defined in a method or a scope. The difference between local inner class and member inner class is that the access of local inner class is limited to the method or the scope.

    class People{
     public People() {
    
     }
    }
    class Man{
     public Man(){
    
     }
    
     public People getWoman(){
         class Woman extends People{   //Local inner class
             int age =0;
         }
         return new Woman();
     }
    }

    Java foundation -- final and static keywordsbe careful: a local inner class is like a local variable in a method. It can’t have public, protected, private and static modifiers.

  3. Anonymous Inner Class

    Anonymous inner class should be the most commonly used when we write code

     class Outer{
         void show() {
             System.out.println("run in Outer");
         }
     }
    
     public class Demo {
         public static void main(String args[]) {
             Outer ot=new Outer(){
             void show() { 
                 System.out.println("run in Inner");
             }
         };
         ot.show();//run in Inner
         }
     }

    Java foundation -- final and static keywordsEssence: it is actually an anonymous object that inherits the class or a subclass of the interface

    • Anonymous inner classes are a further step for local inner classes.
    • The premise of anonymous inner class is that there is a class or interface, and the anonymous inner class is written in the method.
    • It is only used for rewriting one method, and is not recommended for rewriting multiple methods
  4. Static inner class

A static inner class does not need to rely on an outer class, which is similar to the static member property of a class, and it cannot use the non static member variables or methods of an outer class

public class Test {
    public static void main(String[] args)  {
        Outter.Inner inner = new Outter.Inner(); //No need for instance of external class!!! That is, it does not depend on external classes
    }
}
class Outter {
    public Outter() {

    }

    static class Inner { //Only static members and methods of external classes can be accessed!!!
        public Inner() {

        }
    }
}

This work adoptsCC agreementReprint must indicate the author and the link of this article

Leave a Reply

Your email address will not be published.