2

A little side project I've been doing for fun involves subtracting the current date from a future date given by the user to return the days between them.

public int getDaysBetween(int date2)
{
    //Subtract current date from future date (date2), leaving the number of days between them
    int getDaysBetween = 0;
    Calendar myCalendar = Calendar.getInstance();
    myCalendar.get(Calendar.DAY_OF_YEAR); 
    getDaysBetween = date2-Calendar.DAY_OF_YEAR;
    return getDaysBetween;
}

The method for doing this is non-static, as the date2 int changes. However, when I try to reference it in my main class...

//Figure out a non-static reference
    int date2 = Integer.parseInt(JOptionPane.showInputDialog("Enter a day in the year ahead of today"));
    message = "Days bewteen: " + Date.getDaysBetween(date2-Calendar.DAY_OF_YEAR);
    JOptionPane.showMessageDialog(null, message);

I get the error that a non-static method cannot be referenced from a static context.

I am fairly new to Java, so it might seem easy to most of you guys, but I could use the help.

Thanks in advance!

Invictus
  • 23
  • 5
  • 2
    `static` doesn't mean that the method parameter can change, it means that the fields/methods belong to the class, rather than an instance of the class. Thus, you should make your method here `static`. – Jyr May 09 '15 at 19:56

4 Answers4

2

The method for doing this is non-static, as the date2 int changes.

I think you've misunderstood the meaning of the static modifier.

Your method doesn't use any instance fields, and there's no reason to override it in subclasses, so it should be a static method.

date2 is a parameter, so each call to it can pass a different value. That doesn't depend on the instance you call the method on.

(As an aside, it's not really clear what your method is meant to achieve - are you really interested in the day of year? It's also likely that java.time or Joda Time would provide a better API for this. However, it's most important that you understand what static means... you might want to read the Java tutorial on class members.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Your method appears to have intended to return date2 minus the current DAY_OF_YEAR (not minus the DAY_OF_YEAR constant). And if you make it static then you don't need an instance like,

public static int getDaysBetween(int date2) {
    return date2 - Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
}

Assuming this is your own Date class, then to make it non-static (or an instance level) you would need to call it on an instance like

message = "Days bewteen: " + new Date().getDaysBetween(date2);

But if it's static then you can use

message = "Days bewteen: " + Date.getDaysBetween(date2);

Finally, please don't name your class Date (the JRE includes at least two classes with that name java.sql.Date and java.util.Date).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

The method is not static. This means you have to have an instance of the class to use the function. E.g:

Date date = new Date(); // Create an instance of the Date class
date.getDaysBetween(...); // The method call is related to the instance

What you are doing is trying to call the method as if it were static. A static method does not need an instance of the class. Instead it is a feature of a class itself. This if you want to perform a static method call like this:

Date.getDaysBetween(...);

You need to declare the method static:

public static int getDaysBetween(int date2)
Atuos
  • 501
  • 3
  • 12
1

The method for doing this is non-static, as the date2 int changes. Static variable is one that is shared between all the instances of the Class.

Static method is a method that can be invoked without the need for creating and instance on the class.

Variables that cannot be changed are called constants and declared with a keyword "final".

When you declare your method, you can make it static by adding a "static" keyword in method declaration like this:

public static int getDaysBetween(int date2){}

Otherwise, you can keep your method non-static, but in this case, to invoke it, you would have to create an instance of a class and then call method on that instance:

 message = "Days bewteen: " + new Date().getDaysBetween(date2);