Static (class) members cannot access non-static members comprehension

Summarize the basic knowledge points of Java. The explanation is in the comments.

public class staticTest {

    public void play(){
        // method body
    }

    public static void main(String[] args) {
        //main() is a static method, and play() is a non-static method;
        //Static methods are methods of the class itself, called with class.methods, not instances of the class
        //The play() method is an instance method and needs to be called through an instance.

        play(); // wrong

        staticTest st = new staticTest();//Correct
        st.play();

    }
}
Or change the play() method to static.



Guess you like

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