leon
leon

Reputation: 39

If statement Object boolean not working

I'm trying to check for a persons age using another object in another class, for some reason the areThey boolean returns false no matter what. The following code that I implemented is_

  public class main {    
    public static void main(String[] args){    
      Obj object = new Obj();    
      object.areTheyOldEnough();

      if(object.areTheyOldEnough() == true){
          System.out.println("They are old enough!");
      }else{
          System.out.println("They are not old enough!");
      }
    }
}

public class Obj {
    private int age = 15;
    private boolean areThey;

    public boolean areTheyOldEnough() {
        if (age > 12) {
            boolean areThey = true;
        }
        return areThey;    
    } 
} 

Upvotes: 2

Views: 3102

Answers (4)

M2E67
M2E67

Reputation: 970

correct your code as follow :

public class main {

public static void main(String[] args){

  obj Object = new obj();

  if(obj.areTheyOldEnough()){
      System.out.println("They are old enough!");
  } else{
      System.out.println("They are not old enough!");
  }
}

and your obj class will be :

public class obj {
public int age = 15;
// it is not required, but if you need for other methods,
// you can define it
public boolean areThey;

public boolean areTheyOldEnough() {
    return age > 12;
    // or if you need set variable areThey, so use following codes
   /*
      areThey = age > 12;
      return areThey;
   */
}

Upvotes: 0

Vlad Bochenin
Vlad Bochenin

Reputation: 3072

you mixed visibilities of field and local variable

public class obj {

public int age = 15;
public boolean areThey; //initialized to false as default value for boolean

public boolean areTheyOldEnough() {
   if (age > 12) {
       boolean areThey = true; //create new local variable with the same name as field, 
                               // but visible just in scope of if-block
    }
    return areThey; // return value from field
}

Upvotes: 0

GhostCat
GhostCat

Reputation: 140427

Your problem is called shadowing; here:

That most inner declaration of :

if (age > 12) {
  boolean areThey = true;

Is simply wrong, that should read:

areThey = true;

instead. The point is: you are declaring a new variable of that name, and give that the value true. But that variable vanishes in thin air when that if-block is "left" ... and what is returned is the value of the field areThey within your obj class. And that one still has its initial default value of false.

And beyond that: naming is a real problem in your code. Use names that A) comply to java coding standards; so class names start UpperCase for example; B) mean something.

In other words: the name Object doesn't mean anything (besides creating yet another name clash with the java.lang.Object class name). Better call it "testInstance" or something like that - as said: use names that mean something.

Upvotes: 5

Eran
Eran

Reputation: 393791

You have two boolean variables of the same name - one is a local variable, which you are setting in your method (inside the if block), and the other is a an instance variable which you are returning.

You don't need any boolean variable, just write :

public boolean areTheyOldEnough() {
    return age > 12;
}

Upvotes: 0

Related Questions