【问题标题】:Non-static method (method name()) cannot be referenced from a static context. Why?不能从静态上下文引用非静态方法(方法名称())。为什么?
【发布时间】:2012-07-02 04:24:42
【问题描述】:

我真的很困惑!我有 2 个班级,ClubMembership。在 Membership 中我有方法 getMonth(),在 Club 我有 joinedMonth(),它采用参数'month' - 所以用户输入一个月,然后我希望它返回在该特定月份加入的会员。

我正在尝试从 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;
    }
}

【问题讨论】:

    标签: java object methods static call


    【解决方案1】:

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

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

    希望这有帮助。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 您好,非常感谢您的回复!但我不确定你是怎么处理你提到的?
        • 好吧,我这么说吧:Which Membership 实例的getMonth 方法应该被调用?想想这在现实世界中如何运作,如果它对您有帮助:您有一个充满会员信息的文件夹。现在您想知道在特定月份有多少成员加入。你做什么工作?您可能会查看每个条目并计算与您想要的月份匹配的数量。将其表示为程序时,您必须执行完全相同的操作。但是您的程序所做的是查看文件夹一个月然后忽略它。 (getMonth 没有目标,没有返回值)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-02
        • 2011-06-22
        • 2023-03-31
        相关资源
        最近更新 更多