Non-static method (method name()) cannot be referenced from a static context. Why?

前端 未结 3 1864
暖寄归人
暖寄归人 2021-01-15 20:30

I\'m really confused with this! I have 2 classes, Club and Membership. In Membership I have the method, getMonth(), and in

3条回答
  •  余生分开走
    2021-01-15 20:52

    Static method can be accessed using the class name. In the above code, you are trying to access getMonth() method with class name Membership (Membership.getMonth()). But the signature of the getMonth() is public int getMonth(){ ... }, Here this method doesn't contain any static keyword. Because of this you are getting "non-static method getMonth() cannot be referenced from a static context".

    To solve this we should change public int getMonth() to public static int getMonth() or use the object which you created already for Membership class.

    Hope this is helpful.

提交回复
热议问题