1

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.

  • 6
    `getName` is an *instance* method on `Month`. You haven't got an instance of `Month` to call it on. You should look up `static` in whatever Java book or tutorial you're using to learn Java, to find out about the difference between static members and instance members. – Jon Skeet Feb 21 '13 at 16:35
  • 1
    I see that academic integrity is important to you so I won't post an answer with code. But you might want to look at your setMonthNum method a little closer to make sure its doing what you want it to do. – Scott Feb 21 '13 at 16:45
  • Thank you @Scott That helped a ton!! I'm running into another issue I'll hack away at =D Thanks again! – WannaBeDroidProgrammer Feb 21 '13 at 17:42
  • possible duplicate of [What is the reason behind "non-static method cannot be referenced from a static context"?](http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – Jason C Apr 11 '14 at 03:26

4 Answers4

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

}
Arpit
  • 12,767
  • 3
  • 27
  • 40
1

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.

user1897690
  • 169
  • 2
0

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!

0

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)
user1428716
  • 2,078
  • 2
  • 18
  • 37