static and final&static, final modifier, inner class

One, static

1. Static modifier variable:

Variables modified by static are static variables. Memory is allocated directly with class loading, and has nothing to do with instance objects. All classes share static variables (need to consider scope, private, public, etc.). Due to the problem of shared memory areas, in concurrent scenarios Direct use of static variables will cause security problems. The correct way to open it is: lock the function that modifies the static variable;

2. Static modification function:

Static-modified functions belong to the entire class, exist and can be called when the class is loaded]; in addition, static-modified functions cannot be decorated with abstract and must be implemented;

3. Static modified code block

The static code block is executed before the class constructor, specifically:
parent class static code block --> child class static code block --> parent class non-static code block --> parent class construction method --> child class non-static code block Static code block --> subclass construction method

Two, final

1. final modifier variable

When modifying the basic data type (short\int\long\byte\double\float\char\boolean), it means that the variable value is immutable; when modifying the
reference data type, it means that the variable reference is immutable, but the specific content of the referenced object is Can be changed (for example, the value of an attribute of an object is changeable)

2. final modification method

Indicates that this method cannot be overridden by inheritance, which ensures the security of the scheme.

3. final modified class

Indicates that this class cannot be inherited

4. final modified parameters

Parameters cannot be modified inside the function. The main user ensures data security and avoids problems caused by inadvertently changing data.




static modifier:

The static modifier can be used with properties, methods, and inner classes to indicate static. Static variables and static methods in a class can be used with the class name. There is no need to create an object of a class to access the static members of that class.

class StaticText{
    
    
    //静态变量
    static int i=47;
    //静态方法
    static void echo(){
    
    
        System.out.println("There is a Static Method");
     }
    //构造函数
    StaticText(){
    
    
        System.out.println("There is a Constructor");
    }
    //静态域
    static {
    
    
        System.out.println("There is a static block");
    }

}
public class Text {
    
     
    public static void main(String[] args) {
    
    
        StaticText A = new StaticText();
        StaticText B = new StaticText();
        //A.i和B.i指向同一内存空间
        System.out.println(A.i==B.i);
        A.i++;
        //此时还是有A.i==B.i
        System.out.println(B.i);
        //使用类名訪问时首选方法,这不仅仅是由于它强调了变量的static结构,还为编译器进行优化提供了更好的机会
        System.out.println(StaticText.i);
    }
}

执行结果为:
There is a static block
There is a Constructor
There is a Constructor
true
true
48

Memory allocation of static attributes: In a class, a static variable will only have one memory space. Although there are multiple class instances, the static variable in these class instances will share the same memory space.

Static initializer - static block: A static initializer is a static block that exists outside a method in a class. Executed only once when the class is loaded. Usually used to initialize static class properties.

Static variables are initialized when the class is loaded, that is, as long as the class is loaded, it will be initialized whether it is used or not.

The basic rules of static:

  • A static method of a class can only access static properties.
  • A static method of a class cannot directly call a non-static method.
  • If the access control permissions allow, static attributes and methods can be called by adding "." to the object name. It can also be called by adding "." to the instance;
  • The current object does not exist in a static method. Therefore, this cannot be used, and super cannot be used;
  • Static methods cannot be overridden by non-static methods;
  • The constructor does not agree with the declaration as static.

Although when static acts on a certain field, it will definitely change the way data is created (since static fields only have one storage space for each class, while non-static fields have one storage space for each object).

But when static acts on a method. The difference is not that big. An important function of the static method is that it can be called by the class name without creating any object.


final modifier:

When declaring classes, properties, and methods in Java, you can use the keyword final to modify them. The components marked by final have the characteristics of the final state, which means final.

Basic rules for the final modifier:

  • A class marked final cannot be inherited.
  • Methods marked final cannot be overridden by subclasses;
  • Final marked variables (member variables or local variables) become constants. Can only be assigned once.
  • The member variable of the final mark must be assigned at the same time of declaration. If it is not assigned at the time of declaration, then there is only one chance of assignment, and it can only be explicitly assigned in the constructor. Only then can it be used.
  • A local variable marked with final can only be declared without assignment, and then assigned at one time.
  • final is generally used to mark components whose general functions, implementation methods or values ​​cannot be changed arbitrarily. to avoid being misused.

inner class

There is another class defined inside a class (or method, statement block), which is called an inner class, sometimes called a nested class.

Features of inner classes:

  • Internal classes can reflect logical affiliation, and at the same time, they can control the internal class's invisibility to other classes.

  • The scope of member variables of an outer class is the entire outer class. Contains inner classes, but outer classes cannot access private members of inner classes.

  • Logically related classes can be together, which can effectively implement information hiding. Inner classes can directly access members of outer classes.

  • After compilation, inner classes are also compiled into separate classes. The name is in the form of outclass$inclass.

public class Outer{
    
    
    private int size=0;
    class Inner{
    
    
      public int counter = 10;
      public void doStuff(){
    
    
          size++;
      }
    }
    public static void main(String [] args){
    
    
        Outer outer = new Outer();
        Inner inner = outer.new Inner();
        inner.doStuff();
        System.out.println(outer.size);
        System.out.println(inner.counter);   
    }   
}

Internal classes can be divided into four types: 1. Class level: member type. It has static modification; 2. Object level: member type. ordinary. No static modification; 3. Local inner class: local type. 4. Anonymous level: partial type.

Static variables are stored in the method area.

Guess you like

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