Nonstatic method cannot be reference from a Static context

我只是一个虾纸丫 提交于 2019-12-31 04:27:06

问题


My Class is looking like this:

public class Month
 {
private int numOfMonth;
private int monthNum;

public int monthNum()
{
    return monthNum = 1;
}

public void setMonthNum(int monthNum){

    switch (monthNum)
    {
    case 1: System.out.println("January"); break;
    case 2: System.out.println("February");break;
    case 3: System.out.println("March");break;
    case 4: System.out.println("April");break;
    case 5: System.out.println("May");break;
    case 6: System.out.println("June");break;
    case 7: System.out.println("July");break;
    case 8: System.out.println("August");break;
    case 9: System.out.println("September");break;
    case 10: System.out.println("October");break;
    case 11: System.out.println("November");break;
    case 12: System.out.println("December");break;
    }

}

    public String getName() 
    {
        return "" + monthNum;
    }

}

My driver is as follows:

import java.util.Scanner;

public class monthDriver
{
public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);

    System.out.println("Enter month number: ");
    int monthNum = in.nextInt();

    System.out.println("Month number " + monthNum + " is the month of " + Month.getName());

}
 }

I get the compile time error:

"monthDriver.java:12: error: non-static method getName() cannot be referenced from a static context
    System.out.println("Month number " + monthNum + " is the month of " + Month.getName());1 error"

Keeping in mind that I am a student, and academic integrity is important to me, Why am I receiving such an error? Also, are there any suggestion that could be made improve my coding efficiency in the future? Thank you for all your time and effort. It is GREATLY appreciated.


回答1:


Method 1:

You can solve your problem by palcing static like this :

public static String getName() 
    {
        return "" + monthNum;
    }

And you call should be like

System.out.println("Month number " + monthNum + " is the month of " + Month.getName());

Method 2 :

Create object of class Month and then :

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);

    System.out.println("Enter month number: ");
    int monthNum = in.nextInt();
    Month obj=new Month();
    System.out.println("Month number " + monthNum + " is the month of " + obj.getName());

}



回答2:


Well for starters, if you want to access a method of a class (in your case Month) without first instantiating the class itself, but directly with Month.getName(), then that method must be defined as static.

About when to use static or non-static methods in a class, you can find so much articles online to fill up a library :-)

Another small note about the code above. Instead of using a switch you might be interested in using an enumeration.




回答3:


Your code seem a bit raw and messy. I would suggest use:

    public static String getMonthNameForNum(int monthNum) {
    switch (monthNum) {
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
        default:
            return "UNKNOWN";
    }
}

and then use it like:

        System.out.println("Month number " + monthNum + " is the month of " + Month.getMonthNameForNum(monthNum));

P.S. Of course, this should be only educational code, in real life you would use standard Calendar, etc. to get month names!




回答4:


I will not correct your program or enhance your program but you need to understand the fundamental concept and what you are doing wrong.

Sometimes you want to create variables that is common to all Instance of the Class a.k.a Object. Similarly You would also define static methods

Read from this link. I am just pasting the snippet here.

The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, **should be invoked with the class name, without the need for creating an instance of the class, as in

 ClassName.methodName(args)

Not all combinations of instance and class variables and methods are allowed:

Instance methods can access instance variables and instance methods directly.

Instance methods can access class variables and class methods directly.

Class methods can access class variables and class methods directly.

Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.

In your case last Rule applies as this is Instance Method hence you cannot invoke Month.setMonthnum(bla)

  public void setMonthNum(int monthNum)


来源:https://stackoverflow.com/questions/15007456/nonstatic-method-cannot-be-reference-from-a-static-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!