if you create an instance variable in a class, is the default value true or false until otherwise changed?
Is it good practice to have an instance variable as ex. true then change the value to false and use that variable throughout your class? Or is that something you should conceptually avoid in terms of using instance variables?
If you create an instance variable in a class, is the default value true or false until otherwise changed?
The default value is false
. (JLS 4.12.5)
Is it good practice to have an instance variable as ex. true then change the value to false and use that variable throughout your class?
I assume you mean, is it good practice to define your boolean instance variables so that you can rely on default initialization.
The answer is: No. It is not good practice:
It is good practice to define the instance variables so that they make sense to the reader of the code:
// Good (probably)
private boolean isValid = true;
// Bad (probably)
private boolean isNotValid; // so that I can rely on default init
(Now, it may make your code easier to understand if the variable is negated ... but the point is that you should decide based on what makes the code easy to understand ... not on based exploiting default initialization.)
It is bad practice to spend time worrying about performance issues at this level of granularity. The chances are that performance benefit of avoiding an explicit initialization is insignificant.