Non-static method (method name()) cannot be refere

2019-02-20 11:45发布

I'm really confused with this! I have 2 classes, Club and Membership. In Membership I have the method, getMonth(), and in Club I have joinedMonth() which takes the parameter, 'month' - so a user enters a month and then I want it to return the Membership's which joined in that specific month.

I am trying to call the getMonth() method from class Club, so that I can then go on to compare the integers of the months. But, when I try to call the method, I just get the mentioned "non-static method getMonth() cannot be referenced from a static context".

Basically, what is this and how can I resolve it?

Thank you in advance!

Club:

public class Club
{
    private ArrayList<Membership> members;
    private int month;

    /**
     * Constructor for objects of class Club
     */
    public Club()
    {
        // Initialise any fields here ...

    }

    /**
     * Add a new member to the club's list of members.
     * @param member The member object to be added.
     */
    public void join(Membership member)
    {
        members.add(member);
    }

    /**
     * @return The number of members (Membership objects) in
     *         the club.
     */
    public int numberOfMembers()
    {
        return members.size();
    }


        /**
    * Determine the number of members who joined in the given month
    * @param month The month we are interested in.
    * @return The number of members
    */
    public int joinedMonth(int month){

        Membership.getMonth();

    }



}

Membership:

public class Membership
{
    // The name of the member.
    private String name;
    // The month in which the membership was taken out.
    public int month;
    // The year in which the membership was taken out.
    private int year;

    /**
     * Constructor for objects of class Membership.
     * @param name The name of the member.
     * @param month The month in which they joined. (1 ... 12)
     * @param year The year in which they joined.
     */
    public Membership(String name, int month, int year)
        throws IllegalArgumentException
    {
        if(month < 1 || month > 12) {
            throw new IllegalArgumentException(
                "Month " + month + " out of range. Must be in the range 1 ... 12");
        }
        this.name = name;
        this.month = month;
        this.year = year;
    }

    /**
     * @return The member's name.
     */
    public String getName()
    {
        return name;
    }

    /**
     * @return The month in which the member joined.
     *         A value in the range 1 ... 12
     */
    public int getMonth()
    {
        return month;
    }

    /**
     * @return The year in which the member joined.
     */
    public int getYear()
    {
        return year;
    }

    /**
     * @return A string representation of this membership.
     */
    public String toString()
    {
        return "Name: " + name +
               " joined in month " +
               month + " of " + year;
    }
}

3条回答
Anthone
2楼-- · 2019-02-20 12:13

Membership is a class. Calling methods though it is only allowed if the method is static. Your getMonth method isn't static, so you will need an instance of the Membership class to call it. You already have a list of instances in your Club class, so pick one of those and call getMonth on it.

查看更多
Ridiculous、
3楼-- · 2019-02-20 12:18

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.

查看更多
地球回转人心会变
4楼-- · 2019-02-20 12:26

Static modifier make method/field to be part of class not object (instance). You call it by using class name as reference (or object reference, but that is bad practice). If method/field is not static it must to be called via reference to class object (instance).

查看更多
登录 后发表回答