A few notes about static methods

static method aka class method

1 A class method refers to a method in a class that is modified by static, without this pointer; this can be understood as an object, and a class method belongs to a class, not an object, so the this pointer cannot be added before the class method;

     Non-static member functions (instance methods) imply this pointer, static member functions (class methods) lack this pointer

2 The class method can call the static method of other classes. If it is a subclass, the class method of the subclass can call the class method of the parent class (note that it is called! The subclass will not inherit the static method of the parent class, for static There is no such thing as "overriding" for methods in subclasses. As we mentioned earlier, methods and variables modified with the static keyword belong to the class itself. Even if there is an inheritance relationship, subclasses do not There are no statically modified variables and methods inherited from the parent class, so even if the subclass and the parent class have the same static methods and variables, they have nothing to do with each other, they are independent of each other, they belong to their own classes )

 

public class Parent {
    public static void  staticMethod(){
        System.out.println("Parent staticMethod run");

    }

}
public class Son extends Parent {
    //...
}



public class Test {

    public static void main(String[] args) {
        Parent child=new Son();
        child.staticMethod();//输出:Parent staticMethod run   
        Son s=new Son();
        s.staticMethod();//输出:Parent staticMethod run   


    }

 

3   Non-static methods cannot be called in static methods, to be precise, non-static methods cannot be called directly . But you can call a non-static method of an object by passing a reference to an object into a static method.

 

public class A{
       //non-static method in class A

       public void func(){ ...... }
       
        //Static method in class A (main function)

        public static void main(String[] args){
             A a=new A();//The non-static method in A can only be called after the object of A needs to be instantiated
             a.func();
        }

As in the simple example above, when a static method calls a non-static method, the object of the class containing the non-static method needs to be instantiated
Static methods or properties in a class are not essentially members of the class. When the Java virtual machine is installed in the class, these static objects already have objects, and 
they just "resident" in this class, without passing The class's constructor ( constructor ) class is instantiated; non-static properties or methods do not exist when the class is loaded, and the instance object of the class can only be relied on after
the class's constructor is executed.

For more information, please refer
to https://blog.csdn.net/u010412719/article/details/49254017 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889295&siteId=291194637