3

I have a class as shown

package com;

public class Person {
boolean registered;

public boolean isRegistered() {
    return registered;
}

public void setRegistered(boolean registered) {
    this.registered = registered;
}
}

The Data to Person Object will be set based on the Data present in DB . The problem is that for older records the registered filed is not present .

so i how can i test if the filed is present or not ??

package com;

public class Test {

    public static void main(String args[]) {
        Person per = new Person();

        if (per.isRegistered()) {

        }
    }

}

How can i check if the per.isRegistered() field is present or not for that Person Object ??

1
  • You could always just default the registered boolean to false since if the field is not set, you could assume it's not registered. Commented Mar 4, 2013 at 11:35

10 Answers 10

6

The way your Person class is currently defined, you can't, because boolean (the primitive type) has no null value, only true and false.

You could use Boolean (the object equivalent), which can be null, Boolean.TRUE, or Boolean.FALSE.

But more likely you want to solve this at a deeper level. You've said you have data in your database for which this field isn't present. Are those records considered registered or not? Your class should reflect the answer to that question.

Sign up to request clarification or add additional context in comments.

1 Comment

The way it usually happens in practice, the answer is context-dependent and is not a property of the class. Business logic is usually best done with a clean separation of code and data.
5

boolean is primitive data type which can not be null, only can be true or false. But Boolean Object which is rapper of boolean data type, It could be null and Boolean.TRUE and Boolean.FALSE;

public class Person {
    Boolean registered;
    ...
}

...

Person per = new Person();

if (per.isRegistered()!=null ) {
    if(!per.isRegistered()){
         ...
    }else{
         ...
    }
}

Comments

3

boolean can't be null; you'd need to use (Boolean) or construct a composite type.

Comments

1

You need a three state boolean; true, false and not there.

You can achieve this by using Boolean class instead of the primitive boolean and use null as the "not there" indicator.

Comments

1

I don't really get what is on your mind. But if you want to handle a boolean whose values can be one of the following {true/false/null}, you should use a Boolean object with is a simple boolean wrapper, but it can take the null value (as every Java Object can be set to null).

Comments

1

You can use the Boolean (capital B) class which allows NULL values for references of its kind.

Alternatively, you can use an enum having three possible values: NOT_AVAILABLE, YES, NO. This has a clearer semantics and allows you to add other possible values later (if needed).

Comments

1

You should use Optional

if (Optional.ofNullable(per.isRegistered()).orElse(false)) {
  // ...
}

1 Comment

I think this will be very helpful or easy.
0

boolean registered as field it's false by default. Moreover boolean not related to Object class but primitive value so can't be null at all.

Only one Null-pointer you may get here it's when per is null

Comments

0

Make the column nullable and use a Boolean instead of boolean. For the specific problem though, I think you can assume that if it is null the user is not registered.

Comments

0

You have to extend the person class to use it in the test class. You also have to define the boolean.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.