Java问题记录|Non-static method cannot be referenced from a static context

Problem:
Here Insert Picture Description
Reason:
Static methods can not call non-static methods own definition of
resolve:
1. Change the non-static methods as static methods, plus static void before the add method of
Here Insert Picture Description
the object 2 can be instantiated in the main non-static method in the main method
the following example:

public class Demo01 {

    public static void main(String[] args) {
        //静态方法的调用
        Demo01.add();
        //非静态方法的调用
        Demo01 demo=new Demo01();//实例化对象,再调用方法
        demo.get();
    }

    //静态方法
    public static void add(){
        System.out.print("静态方法");
    }

    //非静态方法
    public  void get(){
        System.out.print("非静态方法");
    }

}
Released nine original articles · won praise 0 · Views 110

Guess you like

Origin blog.csdn.net/linanali0606/article/details/104757447