Default values and initialization in Java

JavaInitializationGlobal VariablesDefault ValueLocal Variables

Java Problem Overview


Based on my reference, primitive types have default values and Objects are null. I tested a piece of code.

public class Main {
    public static void main(String[] args) {
        int a;
        System.out.println(a);
    }
}

The line System.out.println(a); will be an error pointing at the variable a that says variable a might not have been initialized whereas in the given reference, integer will have 0 as a default value. However, with the given code below, it will actually print 0.

public class Main {
    static int a;
    public static void main(String[] args) {
        System.out.println(a);
    }
}

What could possibly go wrong with the first code? Do class variables behave different from local variables?

Java Solutions


Solution 1 - Java

In the first code sample, a is a main method local variable. Method local variables need to be initialized before using them.

In the second code sample, a is class member variable, hence it will be initialized to the default value.

Solution 2 - Java

Read your reference more carefully:

> Default Values > > It's not always necessary to assign a value when a field is declared. > Fields that are declared but not initialized will be set to a > reasonable default by the compiler. Generally speaking, this default > will be zero or null, depending on the data type. Relying on such > default values, however, is generally considered bad programming > style. > > The following chart summarizes the default values for the above data > types. > > . . . > > Local variables are slightly different; the compiler never assigns a > default value to an uninitialized local variable. If you cannot > initialize your local variable where it is declared, make sure to > assign it a value before you attempt to use it. Accessing an > uninitialized local variable will result in a compile-time error.

Solution 3 - Java

These are the main factors involved:

  1. member variable (default OK)
  2. static variable (default OK)
  3. final member variable (not initialized, must set on constructor)
  4. final static variable (not initialized, must set on a static block {})
  5. local variable (not initialized)

Note 1: you must initialize final member variables on every implemented constructor!

Note 2: you must initialize final member variables inside the block of the constructor itself, not calling another method that initializes them. For instance, this is not valid:

private final int memberVar;

public Foo() {
    // Invalid initialization of a final member
    init();
}

private void init() {
    memberVar = 10;
}

Note 3: arrays are Objects in Java, even if they store primitives.

Note 4: when you initialize an array, all of its items are set to default, independently of being a member or a local array.

I am attaching a code example, presenting the aforementioned cases:

public class Foo {
    // Static and member variables are initialized to default values

    // Primitives
    private int a; // Default 0
    private static int b; // Default 0

    // Objects
    private Object c; // Default NULL
    private static Object d; // Default NULL

    // Arrays (note: they are objects too, even if they store primitives)
    private int[] e; // Default NULL
    private static int[] f; // Default NULL

    // What if declared as final?

    // Primitives
    private final int g; // Not initialized. MUST set in the constructor
    private final static int h; // Not initialized. MUST set in a static {}

    // Objects
    private final Object i; // Not initialized. MUST set in constructor
    private final static Object j; // Not initialized. MUST set in a static {}

    // Arrays
    private final int[] k; // Not initialized. MUST set in constructor
    private final static int[] l; // Not initialized. MUST set in a static {}

    // Initialize final statics
    static {
        h = 5;
        j = new Object();
        l = new int[5]; // Elements of l are initialized to 0
    }

    // Initialize final member variables
    public Foo() {
        g = 10;
        i = new Object();
        k = new int[10]; // Elements of k are initialized to 0
    }

    // A second example constructor
    // You have to initialize final member variables to every constructor!
    public Foo(boolean aBoolean) {
        g = 15;
        i = new Object();
        k = new int[15]; // Elements of k are initialized to 0
    }

    public static void main(String[] args) {
        // Local variables are not initialized
        int m; // Not initialized
        Object n; // Not initialized
        int[] o; // Not initialized

        // We must initialize them before use
        m = 20;
        n = new Object();
        o = new int[20]; // Elements of o are initialized to 0
    }
}

Solution 4 - Java

There are a few things to keep in mind while declaring primitive type values.

They are:

  1. Values declared inside a method will not be assigned a default value.
  2. Values declared as instance variables or a static variable will have default values assigned which is 0.

So in your code:

public class Main {
    int instanceVariable;
    static int staticVariable;
    public static void main(String[] args) {
        Main mainInstance = new Main()
        int localVariable;
        int localVariableTwo = 2;
        System.out.println(mainInstance.instanceVariable);
        System.out.println(staticVariable);
       // System.out.println(localVariable); // Will throw a compilation error
        System.out.println(localVariableTwo);
    }
}

Solution 5 - Java

Yes, an instance variable will be initialized to a default value. For a local variable, you need to initialize before use:

public class Main {

    int instaceVariable; // An instance variable will be initialized to the default value

    public static void main(String[] args) {
        int localVariable = 0; // A local variable needs to be initialized before use
    }
}

Solution 6 - Java

Local variables do not get default values. Their initial values are undefined without assigning values by some means. Before you can use local variables they must be initialized.

There is a big difference when you declare a variable at class level (as a member, i.e., as a field) and at the method level.

If you declare a field at the class level they get default values according to their type. If you declare a variable at the method level or as a block (means any code inside {}) do not get any values and remain undefined until somehow they get some starting values, i.e., some values assigned to them.

Solution 7 - Java

All member variables have to load into the heap, so they have to be initialized with default values when an instance of class is created.

In case of local variables, they don't get loaded into the heap. They are stored on the stack until they are being used. This is before Java 7, so we need to explicitly initialize them.

Solution 8 - Java

In Java, the default initialization is applicable for only instance variable of class member.

It isn't applicable for local variables.

Solution 9 - Java

I wrote following function to return a default representation 0 or false of a primitive or Number:

/**
 * Retrieves the default value 0 / false for any primitive representative or
 * {@link Number} type.
 *
 * @param type
 *
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T getDefault(final Class<T> type)
{
	if (type.equals(Long.class) || type.equals(Long.TYPE))
		return (T) new Long(0);
	else if (type.equals(Integer.class) || type.equals(Integer.TYPE))
		return (T) new Integer(0);
	else if (type.equals(Double.class) || type.equals(Double.TYPE))
		return (T) new Double(0);
	else if (type.equals(Float.class) || type.equals(Float.TYPE))
		return (T) new Float(0);
	else if (type.equals(Short.class) || type.equals(Short.TYPE))
		return (T) new Short((short) 0);
	else if (type.equals(Byte.class) || type.equals(Byte.TYPE))
		return (T) new Byte((byte) 0);
	else if (type.equals(Character.class) || type.equals(Character.TYPE))
		return (T) new Character((char) 0);
	else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE))
		return (T) new Boolean(false);
	else if (type.equals(BigDecimal.class))
		return (T) BigDecimal.ZERO;
	else if (type.equals(BigInteger.class))
		return (T) BigInteger.ZERO;
	else if (type.equals(AtomicInteger.class))
		return (T) new AtomicInteger();
	else if (type.equals(AtomicLong.class))
		return (T) new AtomicLong();
	else if (type.equals(DoubleAdder.class))
		return (T) new DoubleAdder();
	else
		return null;
}

I use it in hibernate ORM projection queries when the underlying SQL query returns null instead of 0.

/**
 * Retrieves the unique result or zero, <code>false</code> if it is
 * <code>null</code> and represents a number
 *
 * @param criteria
 *
 * @return zero if result is <code>null</code>
 */
public static <T> T getUniqueResultDefault(final Class<T> type, final Criteria criteria)
{
	final T result = (T) criteria.uniqueResult();

	if (result != null)
		return result;
	else
		return Utils.getDefault(type);
}

One of the many unnecessary complex things about Java making it unintuitive to use. Why instance variables are initialized with default 0 but local are not is not logical. Similar why enums dont have built in flag support and many more options. Java lambda is a nightmare compared to C# and not allowing class extension methods is also a big problem.

Java ecosystem comes up with excuses why its not possible but me as the user / developer i dont care about their excuses. I want easy approach and if they dont fix those things they will loose big in the future since C# and other languages are not waiting to make life of developers more simple. Its just sad to see the decline in the last 10 years since i work daily with Java.

Solution 10 - Java

In the first case you are declaring "int a" as a local variable(as declared inside a method) and local varible do not get default value.

But instance variable are given default value both for static and non-static.

Default value for instance variable:

int = 0

float,double = 0.0

reference variable = null

char = 0 (space character)

boolean = false

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael &#39;Maik&#39; ArdanView Question on Stackoverflow
Solution 1 - JavaJuned AhsanView Answer on Stackoverflow
Solution 2 - JavaPaoloView Answer on Stackoverflow
Solution 3 - JavaKostas ChalkiasView Answer on Stackoverflow
Solution 4 - JavaPritam BanerjeeView Answer on Stackoverflow
Solution 5 - JavaupogView Answer on Stackoverflow
Solution 6 - JavaAniView Answer on Stackoverflow
Solution 7 - JavanewuserView Answer on Stackoverflow
Solution 8 - JavarameshView Answer on Stackoverflow
Solution 9 - JavadjmjView Answer on Stackoverflow
Solution 10 - JavaMohitView Answer on Stackoverflow