Skip to content

Variables

  • defaults for the variable types

    • byte, short, int, long = 0
    • float, double = 0.0
    • char = \u0000
    • boolean = false
    • reference = null
  • All primitive local variables (stack allocated memory space) need to be initialized explicitly, during declaring or seperately before use, otherwise they will raise compile-time error when used.

  • All primitive array of local variables (heap allocated memory space) need to be allocated memory space using new operator, otherwise the data elements will not be default to the default values and no allocation of the same will raise to compile-time error.

Variable TypeCategoryExampleRemarks
Local VariablePrimitiveint i;Not initialized, throws compile-time error
Local VariableArray of Primitivesint[] i;Not initialized, throws compile-time error(no memory allocated)
Local VariableArray of Primitivesint[] i = new int[1];defaulted elements to 0
Local VariableWrapperInteger i;Not initialized, throws compile-time error
Local VariableArray of WrapperInteger[] i;Not initialized, throws compile-time error(no memory allocated)
Local VariableArray of WrapperInteger[] i = new Integer[1];No Error, defaulted elements to null
Local VariableobjectsCustomObject i;Not initialized, throws compile-time error
Local VariableArray of objectsCustomObject[] i;Not initialized, throws compile-time error(no memory allocated)
Local VariableArray of objectsCustomObject[] i = new CustomObject[1];No Error, defaulted elements to null

Variable TypeCategoryExampleRemarks
Instance/Static VariablePrimitiveint i;Not initialized, no Error, Default value to 0
Instance/Static VariableArray of Primitivesint[] i;Not initialized, no memory allocated, default value to null
Instance/Static VariableArray of Primitivesint[] i = new int[1];No Error, defaulted elements to 0
Instance/Static VariableWrapperInteger i;Not initialized, no memory allocated, default value to null
Instance/Static VariableArray of WrapperInteger[] i;Not initialized, no memory allocated, default value to null
Instance/Static VariableArray of WrapperInteger[] i = new Integer[1];memory allocated, default elements value to null
Instance/Static VariableobjectsCustomObject i;Not initialized, no memory allocated, default value to null
Instance/Static VariableArray of objectsCustomObject[] i;Not initialized, no memory allocated, default value to null
Instance/Static VariableArray of objectsCustomObject[] i = new CustomObject[1];memory allocated, default elements value to null
  • Local Variables
    • MUST be initialized before use, otherwise compile-time error
    • created in the stack memory area, for references objects, the object lives in heap but the type lives in stack
    • final is the ONLY modifier which can be used with local variables
  • Instance Variables
    • eligible for GC, once they go out of scope.
    • scoped to object
    • ONLY accessed/modified within a non-static block
    • automatically initialized to default values
    • if declared as final, MUST be initialized in decleration or constructor or instance initializer block
  • Static Variables
    • Global var, available to all programs running in the JVM instance
    • loaded into memory, when the class loads, even before the object creation
    • NOT participate or eligible for GC
    • automatically initialized to default values
    • if declared as final, MUST be initialized in decleration or static initializer block
  • Order of Initialization : Static variable > Static Initializer block > Instance Variable > Instance Initializer block > Instance Constructor