简体   繁体   中英

Java check if a boolean is initialized

I'm not talking about checking if a boolean is null as a means to check if it is initialized. Because that won't work. So I thought this might work:

public class MyClass{

    private boolean setting;

    private boolean getSetting{
        // get the setting;
    }

    public void main{
        if (setting != (true && false))
            setting = getSetting();
        // do main stuff;
    }

}

And it does! So my question is: would this be a good practice compared to the alternative, which would be the use of an extra variable:

public class MyClass{

    private boolean initialized = false;
    private boolean setting;

    private boolean getSetting{
        // get the setting;
    }

    public void main{
        if (!initialized)
            setting = getSetting();
            initialized = true;
        // do main stuff;
    }

}

Booleans are a primitive type, therefore they do not need to be initialized. The default value for a boolean is false .

The first code sample returns false because the phrase true && false will always equate to false ( && checks whether both statements are true, which they aren't, so it is false) and then you check if setting , which is also false , is unequal to that, which it is not. An initialized variable could therefore be useful.

You can read more about defaults and primitive types here .

if (setting != (true && false)) doesn't do what you think it's doing.

The expression (true && false) always evaluates to false : you're doing a logical AND on the literal values true and false .

That means that your test reduces to if (setting != false) . Since setting is not initialized, it defaults to false . When the code runs it checks if (false != false) , which evaluates to false .

When I run the code you posted it does not call getSetting , as expected.

In your example, best practice would be to initialize the variable inside the constructor:

public class MyClass {
    private boolean someSetting;

    public MyClass() {
        someSetting = getSetting();
    }

    private boolean getSetting() {
        ...
    }

    public void main() {
        // By the time this is called we can guarantee that someSetting has been initialized correctly
        // Do main stuff...
    }
}

The issue with (setting != (true && false)) has already been discussed, but the real issue is that the primitive boolean only has its two obvious options. It's always either true or false . If both of those are meaningful answers, then that value cannot also indicate whether it's been initialized or not. An initialized, meaningful false will look the same as not having been initialized.

The best solution if it's available is, as Cameron Skinner answered above, to ensure the variable is always initialized by doing so either where it's declared or in the constructor. That, however, requires that the answer to what setting should be is known at the time the class is instantiated.

If the proper value is not necessarily known at that point and might need to be set later (so having been deliberately set to false is a different situation than having never been set), then you have a couple options:

One is as shown in the initial post, to have a separate initialized variable telling you whether it's happened or not. That might look a little clunky, but it works.

The other option is to use a Boolean object rather than a boolean primitive for setting . A Boolean object can be set to either true or false , but it initially defaults to null , so you can use a check for null to determine whether it has been set or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM