Detailed explanation of static related knowledge points

1. Modify member variables

A member variable modified by static is called a static member variable. This variable does not belong to a specific object and is shared by all objects.

public class Student {
    
    
    private String name;
    private static String classRoom="106";

    public static void main(String[] args) {
    
    
        // 方法一:静态成员变量可以直接通过类名访问
        System.out.println(Student.classRoom);

        //方法二:静态成员变量也可以通过对象访问,但是classRoom是三个对象共享的
        Student s1=new Student();
        Student s2=new Student();
        Student s3=new Student();
        System.out.println(s1.classRoom);
        System.out.println(s2.classRoom);
        System.out.println(s3.classRoom);

    }
}

insert image description here

From the above code we can see:

Static member variable attributes】:

  1. **It does not belong to a specific object, it is an attribute of a class, shared by all objects, and not stored in the space of an object.
  2. It can be accessed by object or by class name, but it is generally recommended to use class name to access.
  3. Class variables are stored in the method area .
  4. The life cycle accompanies the lifetime of the class (that is, it is created with the loading of the class and destroyed with the unloading of the class).

2. Modify member methods

A member method modified by static is called a static member method, which is a method of a class, not unique to an object. Outside a class, static members are typically accessed through static methods.

public class Student {
    
    
    private String name;
    private static String classRoom="106";

    public static String getClassRoom(){
    
    
        return classRoom;
    }
}
class TestStudent{
    
    
    public static void main(String[] args) {
    
    
        Student s1=new Student();
        System.out.println(Student.getClassRoom());

    }
}

insert image description here
From the above code we can see:

static method attribute】:

  1. It does not belong to a specific object, it is a class method.
  2. It can be called by object, or by **class name. static method name(...)**, the latter is more recommended.

insert image description here
insert image description here

  1. Cannot access any non-static member variables in a static method

insert image description here
insert image description here

  1. A static method cannot call any non-static method.
  2. Static methods cannot be overridden and polymorphism cannot be achieved.

3. Modified code block

Static member variables are generally not initialized in the constructor, and the instance attributes related to the object are initialized in the constructor. So how to realize the initialization of static member variables?

1. In-place initialization: the initial value is given directly at the time of definition

private static String classRoom="106";

2. Static code block initialization

A code block defined using static is called a static code block. Generally used to initialize static member variables.

public class Student {
    
    
    private String name;
    private static String classRoom;

    static {
    
    
        classRoom="106";
    }

    public static void main(String[] args) {
    
    
        System.out.println(Student.classRoom);
    }
}

insert image description here
Here are a few things to note

  • No matter how many objects are generated in the static code block, it will only be executed once.
  • Static member variables are attributes of the class, so they are created and initialized when the JVM loads the class .
  • If a class contains multiple static code blocks, when compiling the code, the compiler will execute (merge) the instance code blocks sequentially according to the order of definition and will only be executed when the object is created.

Four. Modification

The inner member class modified by static is called static inner class.

public class OutClass {
    
    
    private int a;
    private static int b;
    public void methodA(){
    
    
        a=10;
        System.out.println(a);
    }
    public static void methodB(){
    
    
        System.out.println(b);
    }

    //静态内部类
    static class InnerClass{
    
    
        public void methodInner(){
    
    

            //a=100;//编译失败,在内部类中只能访问外部类的静态成员变量
            b=200;
            //methodA();//编译失败,因为methodB()不是静态成员方法
            methodB();

        }
    }

    public static void main(String[] args) {
    
    
        //静态内部类对象创建
        OutClass.InnerClass innerClass=new OutClass.InnerClass();
        innerClass.methodInner();
    }
}

insert image description here
Here are a few points to note:

  • Only static members in the outer class can be accessed in the static inner class.
  • When creating a static inner class object, there is no need to create an outer class object first.

What if we must access non-static members in external classes?

We can create an external class object in a static inner class and then access it through this object.

The code example is as follows:

static class InnerClass{
    
    
        public void methodInner(){
    
    
           OutClass outClass=new OutClass();
           outClass.a=10;
           outClass.methodA();

  		}
}

insert image description here

Guess you like

Origin blog.csdn.net/m0_63904107/article/details/132435671
Recommended