Note that there are some explanatory texts on larger screens.

plurals
  1. POMy boolean of an object gets changed without the program doing anything
    text
    copied!<p>I've been debugging for hours, and I finally found where the problem is. NOW I have to fix it :)</p> <p>I thinks something strange happens. </p> <p>I'm creating an date app, where I calculate which day it is (with leapyear corrections etc). </p> <p>I have a method, where I take a Year object. </p> <pre><code>private int totalDays(Year yearnumber) { System.out.println("Boolean check 1: " + yearnumber.getLeapYear()); //calculate days for whole year// int daysWholeYear = 0; for (int i = year.getYearZero(); i &lt; yearnumber.getYear(); i++) { // here i will add all the days (366) from the leapyears // if (yearnumber.isLeapYear(i) == true) { totalDays += year.getLengthyear() + 1; System.out.println("Boolean check 2: " + yearnumber.getLeapYear()); } else { totalDays += year.getLengthyear(); } } System.out.println("Boolean check 3: " + yearnumber.getLeapYear()); </code></pre> <p>My first two boolean checks are ok.</p> <p>Code (without the boolean check looped in the for loop)</p> <pre><code>Boolean check 1: true Boolean check 2: true Boolean check 3: false </code></pre> <p>I need my Boolean in the next lines of my method, where I calculate the days of the months (non whole years). However, my program now thinks that the year is not a leap year and therefore makes wrong calculations. </p> <p>Because this Boolean changes in my program, the rest of my calculation are off. Can someone explain my why this happens? :)</p> <p>EDIT: code from my year class: </p> <pre><code>public class Year { private static int yearzero = 1753; private static int lengthYear = 365; private int year; private boolean leapYear; private int startYear; //are used for an interval calculations private int eindYear; // public Year(int year) { this.year = year; this.leapYear = isLeapYear(year); } boolean isLeapYear(int year) { return leapYear = (year % 400 == 0) || ((year % 100) != 0 &amp;&amp; (year % 4 == 0)); } public int getYear(){ return year; } public int getYearzero () { return yearZero; } public int getLengthYear() { return lengthYear; } public boolean getLeapYear() { return leapYear; } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload