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

Java 无法从静态上下文引用非静态方法(方法名称())。为什么?,java,object,methods,static,call,Java,Object,Methods,Static,Call,我真的被这搞糊涂了!我有两门课,俱乐部和会员。在Membership中我有方法,getMonth(),在Club中我有joinedMonth(),它接受参数“month”-因此用户输入一个月,然后我希望它返回在该特定月份加入的会员资格 我试图从class Club调用getMonth()方法,这样我就可以继续比较月份的整数。但是,当我尝试调用该方法时,我只得到了前面提到的“非静态方法getMonth()不能从静态上下文引用” 基本上,这是什么?我如何解决它 提前谢谢你 俱乐部: 公共班级俱乐部

我真的被这搞糊涂了!我有两门课,俱乐部会员。在Membership中我有方法,getMonth(),在Club中我有joinedMonth(),它接受参数“month”-因此用户输入一个月,然后我希望它返回在该特定月份加入的会员资格

我试图从class Club调用getMonth()方法,这样我就可以继续比较月份的整数。但是,当我尝试调用该方法时,我只得到了前面提到的“非静态方法getMonth()不能从静态上下文引用”

基本上,这是什么?我如何解决它

提前谢谢你

俱乐部:

公共班级俱乐部
{
私人ArrayList成员;
私人整数月;
/**
*类俱乐部对象的构造函数
*/
公共俱乐部()
{
//在这里初始化任何字段。。。
}
/**
*将新成员添加到俱乐部的成员列表中。
*@param member要添加的成员对象。
*/
公开作废加入(会员)
{
成员。添加(成员);
}
/**
*@返回中的成员数(成员对象)
*俱乐部。
*/
public int numberOfMembers()
{
返回成员。size();
}
/**
*确定在给定月份加入的成员数
*@param 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;
    }
}
公共类成员资格
{
//成员的名称。
私有字符串名称;
//取消会员资格的月份。
公众月;
//取消会员资格的年份。
私人国际年;
/**
*类成员身份的对象的构造函数。
*@param name成员的名称。
*@param month他们加入的月份。(1…12)
*@param year他们加入的年份。
*/
公共成员资格(字符串名称、整数月、整数年)
抛出IllegalArgumentException
{
如果(月<1 | |月>12){
抛出新的IllegalArgumentException(
“月”+月+“超出范围。必须在范围1…12”)内;
}
this.name=名称;
本月=月;
今年=年;
}
/**
*@返回成员的姓名。
*/
公共字符串getName()
{
返回名称;
}
/**
*@返回成员加入的月份。
*范围为1…12的值
*/
公共整数getMonth()
{
返回月份;
}
/**
*@返回成员加入的年份。
*/
公共int getYear()
{
回归年;
}
/**
*@返回此成员身份的字符串表示形式。
*/
公共字符串toString()
{
return“Name:+Name”+
“月内加入”+
月+年+月+年;
}
}

会员资格是一个类别。调用方法,但仅当方法是静态的时才允许。您的
getMonth
方法不是静态的,因此需要
Membership
类的实例来调用它。您的
俱乐部
类中已经有一个实例列表,因此请从中选择一个,并在其上调用
getMonth

会员资格
是一个类。调用方法,但仅当方法是静态的时才允许。您的
getMonth
方法不是静态的,因此需要
Membership
类的实例来调用它。您的
俱乐部
类中已经有一个实例列表,因此选择其中一个并在其上调用
getMonth

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

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

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

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


希望这有帮助。

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

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


希望这对您有所帮助。

您好,非常感谢您的回复!但是我不确定你是如何实现你提到的?好吧,让我这样说:应该调用哪个
成员资格
实例的
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;
    }
}