Why does the Boolean object have a public constructor in Java?
Usually, you will want to use valueOf(boolean)
or even the Boolean.TRUE
/ Boolean.FALSE
constants directly.
But think of a scenario where you want to use a private Boolean
variable as a monitor for synchronizing threads. There you will need to make sure you use your own instance and have full control of it.
valueOf()
only got added in Java 1.4, so it would appear that the constructors exist for backwards compatibility.
This ticket explains the reasons for not deprecating the constructors:
Due to the disruption deprecating an API can have, currently an API has to be "actively hazardous" to be deprecated, like Thread.stop. While the use this constructor is certainly ill-advised, it doesn't rise (or sink) to the standard of hazardousness to be deprecated in the JDK. In the future we may add a "denigration" facility to mark API elements that aren't quite so bad that they should be deprecated, but shouldn't be used in most cases. This constructor would be a good candidate for denigration.
I can't think of a realistic scenario where using Boolean
constructors would be the best way to do something useful.