无法从静态上下文引用非静态方法(方法名称())。为什么?

时间:2012-07-01 12:50:50

标签: java object methods static call

我真的很困惑!我有2个班级,分会会员。在会员资格中我有方法, getMonth(),在俱乐部我有 joinedMonth(),它接受参数'月' - 所以用户输入一个月然后我希望它能够返回在特定月份加入的会员资格。

我正在尝试从类Club调用getMonth()方法,以便我可以继续比较几个月的整数。但是,当我尝试调用该方法时,我只是得到了提到的“非静态方法getMonth()无法从静态上下文中引用”。

基本上,这是什么以及如何解决?

提前谢谢!

俱乐部:

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();

    }



}

成员:

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 个答案:

答案 0 :(得分:4)

Membership是一个班级。只有在方法是静态的情况下才允许调用方法。您的getMonth方法不是静态的,因此您需要Membership类的实例来调用它。您已经在Club课程中列出了实例列表,因此请选择其中一个并在其上调用getMonth

答案 1 :(得分:0)

静态修饰符使方法/字段成为类而非对象(实例)的一部分。您可以通过使用类名作为引用(或对象引用,但这是不好的做法)来调用它。如果方法/字段不是静态的,则必须通过引用类对象(实例)来调用它。

答案 2 :(得分:0)

可以使用类名访问静态方法。在上面的代码中,您尝试使用类名成员资格(Membership.getMonth())访问 getMonth()方法。但是getMonth()的签名是 public int getMonth() {...},这里这个方法不包含任何静态关键字。因此,你得到“非静态方法getMonth()不能从静态上下文中引用”。

要解决这个问题,我们应该将public int getMonth()更改为public static int getMonth(),或者使用您为Membership类创建的对象。

希望这有用。