Question

  1. if you create an instance variable in a class, is the default value true or false until otherwise changed?

  2. 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?

Was it helpful?

Solution

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.

OTHER TIPS

JLS 4.12.5

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):

    • For type byte, the default value is zero, that is, the value of (byte)0.

    • For type short, the default value is zero, that is, the value of (short)0.

    • For type int, the default value is zero, that is, 0.

    • For type long, the default value is zero, that is, 0L.

    • For type float, the default value is positive zero, that is, 0.0f.

    • For type double, the default value is positive zero, that is, 0.0d.

    • For type char, the default value is the null character, that is, '\u0000'.

    • For type boolean, the default value is false.

    • For all reference types (§4.3), the default value is null.

if you create an instance variable in a class, is the default value true or false until otherwise changed?

If the variable is type of boolean, then default value is false, primitives variables default value id 0, object variable/reference variable default value is null

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?

You can do this, it's depends on your requirement.

if you create an instance variable in a class, is the default value true or false until otherwise changed?

If the member is primitive , then false. If it is a wrapper, then null

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?

Setting true or false will be depends on your class context. No issues with it at all.

For ex: You are creating an customer Object and having an instance memeber isActive. If your design allows that all customers by default active then yo are right.

when you crate a variable of boolean   
public class check  {
static boolean  b;
    public static void main(String args[]) {
            System.out.print("The Default Value of Boolean is="+b);
     }
   }

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top