'Why does my boolean value remain as false despite meeting the if condition in my setter?

public class Main {
    public static void main(String[] args) {
        // test1 where all variables are valid
        Main test1 = new Main("David", "male", 2);
        System.out.println(test1);
    }
    private String name;
    private String gender;
    private int noOfEyes;
    private boolean human;

    public Main(){
        setHuman(false);
    }

    public Main(String isName, String isGender, int isNoOfEyes){
        setName(isName);
        setGender(isGender);
        setNoOfEyes(isNoOfEyes);
    }
    public String getName(){
        return name;
    }
    public String getGender(){
        return gender;
    }
    public int getNoOfEyes(){
        return noOfEyes;
    }
    public boolean getHuman(){
        return human;
    }

    public boolean setName(String newName){
        boolean flag =false;
        if(newName.length() > 1){
            name = newName;
            flag = true;
        }
        else{
            flag = false;
        }
        return flag;
    }
    public boolean setGender(String newGender){
        boolean flag =false;
        if(newGender.length() > 1){
            gender = newGender;
            flag = true;
        }
        else{
            flag = false;
        }
        return flag;
    }
    public boolean setNoOfEyes(int newNoOfEyes){
        boolean flag =false;
        if(newNoOfEyes == 2){
            noOfEyes = newNoOfEyes;
            flag = true;
        }
        else{
            flag = false;
        }
        return flag;
    }
    public boolean setHuman(boolean newHuman) {
        boolean flag = false;
        if(name == null || gender == null || noOfEyes == 0) {
            human = newHuman;
            flag = true;
        }
        else {
            flag = false;
        }
        return flag;
    }
    public String toString(){
        String output = "";
        output += "Name is: " + getName() + "\n";
        output += "Gender is: " + getGender() + "\n";
        output += "No of eyes is: " + getNoOfEyes() + "\n";
        output += "Is Human?: " + getHuman() +"\n";

        return output;

    }

}

I am aware that boolean instance variable will auto instan. as false. But since the setHuman if condition is met, as the name is not null,gender is not null and noOfEyes is not 0, should not the boolean human value change to true?

The boolean instance variable is not in the constructor as I am not allowed to do so



Solution 1:[1]

As you pointed out in your question, booleans default to false. That includes the boolean human which is not set in your three argument constructor so it uses the default value.

But why you are returning booleans from your setters anyway? Your class definition is not very conventional. Why not just do the following for your fields.

public void setGender(String gender) {
    this.gender = gender;
}
public String getGender() {
    return gender;
}

public void setHuman(boolean value) {
     this.human = human;
}   

public boolean isHuman() {
   return human;
}    

And as you may be new to Java, a better solution for gender would be to establish an enum of permitted genders. That would ensure that something like tree or table can't be passed (as it could if you used a String). Here is how it might work.

enum Gender {MALE, FEMALE, TRANS, NONBINARY} // etc.
private Gender gender; // class field
public void setGender(Gender gender) {
    this.gender = gender;
}

public Gender getGender() {
    return gender;
}

To set it to male for example, you would do.

setGender(Gender.MALE);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1