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!

1
  • 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. Commented May 9, 2015 at 19:56

4 Answers 4

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.)

Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.