4
votes

I have an assignment for school to make a program which results in either true or false. It's about wether a year is a leap year or not. The problem I have at the moment is that i'm using a public static boolean instead of a public boolean. This is my code:

public class Assignment {

    static boolean isLeapYear;

    public static void main(String[] args)
    {        
        int year = 2000;
        isLeapYear(year);
    }
    public static boolean isLeapYear(int year) {
        if (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0))
            isLeapYear = true;

        else
            isLeapYear = false;

        System.out.println(isLeapYear);

        return isLeapYear;
    }
}

The int year is 2000 at the moment but the rules are like this: A leap year is a year wich can be divided by 4 unless the year is the beginning of a new century (1700, 1800, 1900.....). So even though you can divide 1900 by 4 you can't divide it by 400 so it's false. So again the question: What do I need to do so i'm able to use a public boolean instead of a public static boolean?

4
To use public boolean you need to create an object. - gmustudent

4 Answers

2
votes

You don't need to store this result anywhere.

Use:

public static boolean isLeapYear(int year) 
{
   return (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0));
}
2
votes

You would need to create an instance of your class to invoke that method from your main method, if you want to make your method non-static. And then you can make your isLeapYear variable non-static: -

boolean isLeapYear;
public static void main(String[] args)
{        
    int year = 2000;
    new Assigment().isLeapYear(year);
}
public boolean isLeapYear(int year) {
    // access isLeapYear as `this.isLeapYear` or just `isLeapYear`
}

But, precisely, you don't need to store your result in a boolean variable. If you want to return a boolean value of some expression, then you can just return that expression.

So, just having this code in your method would also work fine, and it is more readable, and let that method be static: -

return (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0))

And from your main call: -

System.out.println("Year : " + year + ", is leap year: " + isLeapYear(year));
1
votes

Static methods can only access static variables, only instance methods can access instance methods, which you can infer if you think Object oriented.

Just in case you should store the Boolean isLeapYear

public class Testing {
boolean isLeapYear;

public static void main(String[] args)
{        
    int year = 2000;
    new Testing().isLeapYear(year);
}
public boolean isLeapYear(int year) {
    if (((year/100)%4 == 0 && year%4 ==0) || (year % 400 == 0))
        isLeapYear = true;

    else
        isLeapYear = false;

    System.out.println(isLeapYear);

    return isLeapYear;
}
}
0
votes

Does your assignment say it has to be stored in a class or instance variable? If not, there is no need for public boolean isLeapYear or public static boolean isLeapYear, just return the result of the calculation and store it in a local variable like this:

public static boolean isLeapYear(int year) {
    return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); 
}

in main method:

int year = 2000;
boolean isLeap = isLeapYear(year);
System.out.println(isLeap);