Changing static boolean

23,786

Solution 1

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

Solution 2

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

Solution 3

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;
}
}
Share:
23,786
TH3Mitch
Author by

TH3Mitch

Updated on November 21, 2020

Comments

  • TH3Mitch
    TH3Mitch over 3 years

    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?

  • Rohit Jain
    Rohit Jain over 11 years
    @Downvoter.. Please leave a comment. I have answered the question that OP asked: - What do I need to do so i'm able to use a public boolean instead of a public static boolean?. Please read the question before downvoting? And I don't think this answer really needs a downvote.
  • Rohit Jain
    Rohit Jain over 11 years
    For sure this is a good suggestion, but it doesn't really answer what OP is asking. He is asking how to use non-static method instead of static method.