Change static boolean

I have a task for the school to make a program that leads to truth or falsehood. This is about a year - this is a leap year or not. The problem that I now have in mind is that I am using public static boolean instead of 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; } } 

This year 2000 is coming, but the rules are as follows: A leap year is a year that can be divided by 4, unless the year is the beginning of a new century (1700, 1800, 1900 .....). Therefore, although you can divide 1900 by 4, you cannot divide it by 400 so that it is false. So again, the question is: what do I need to do so that I can use the public boolean value, and not the public static boolean?

+4
source share
5 answers

You will need to create an instance of your class to call this method from the 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, for sure, you do not need to save your result in a boolean variable. If you want to return the boolean value of some expression, you can simply return this expression.

So, just the presence of this code in your method will also work fine, and it is more readable, and let this 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)); 
+2
source

You do not need to save this result anywhere.

Using:

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

Static methods can only access static variables, only instance methods can access instance methods, which you can do if you consider Object oriented.

Just in case, you should keep 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; } } 
+1
source

Does your job indicate that it should 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 save it in a local variable as follows:

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

in main :

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

The static methods are accessed by calling them from the class name as follows:

 Assignment.isLeapYear(1990) 

Non- static methods are accessed from an instance of the class as follows:

 Assignment a = new Assignement(); a.isLeapYear(1990); 

Static methods can only access static instance variables. To use a non-static instance variable, you need to make your function regular and instantiate the class.

0
source

Source: https://habr.com/ru/post/1447329/


All Articles