Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uninitialized class members in Java do not issue any compiler errors. local variables however do. Why?

Tags:

java

Consider the following code snippet in Java. It won't compile.

package temppkg;

final public class Main
{
    private String x;
    private int y;

    private void show()
    {
        String z;
        int a;

        System.out.println(x.toString()); // Causes a NullPointerException but doesn't issue a compiler error.
        System.out.println(y); // Works fine displaying its default value which is zero.
        System.out.println(z.toString()); // Causes a compile-time error - variable z might not have been initialized.
        System.out.println(a); // Causes a compile-time error - variable a might not have been initialized.
    }

    public static void main(String []args)
    {
        new Main().show();
    } 
}

Why do the class members (x and y) declared in the above code snippet not issue any compile-time error even though they are not explicitly initialized and only local variables are required to be initialized?

like image 764
Lion Avatar asked Dec 15 '11 14:12

Lion


People also ask

Why uninitialized variables are initialized to zero in Java?

Answer Wiki. Yeah in Java , uninitialized variables automatically initialized to zero but if the variable is an instance variable and a class variable because if we don’t initialize the instance variable the system by default initialize them to their respective default values. Following are the instance variable with their default values.

What happens if a local variable is not initialized?

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.

What happens if a field is not initialized in Java?

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. Can variables be used in Java without initialization?

Why is'C'initialized in constructor but not in constructor?

So your fields 'a' and 'b' are both initiated to null, and 'a' is re-initiated in the constructor. This process is not relevant for method calling, so local variable 'c' is neverinitialized.


1 Answers

When in doubt, check the Java Language Specification (JLS).

In the introduction you'll find:

Chapter 16 describes the precise way in which the language ensures that local variables are definitely set before use. While all other variables are automatically initialized to a default value, the Java programming language does not automatically initialize local variables in order to avoid masking programming errors.

The first paragraph of chapter 16 states,

Each local variable and every blank final field must have a definitely assigned value when any access of its value occurs....A Java compiler must carry out a specific conservative flow analysis to make sure that, for every access of a local variable or blank final field f, f is definitely assigned before the access; otherwise a compile-time error must occur.

The default values themselves are in section 4.12.5. The section opens with:

Each class variable, instance variable, or array component is initialized with a default value when it is created.

...and then goes on to list all the default values.

The JLS is really not that hard to understand and I've found myself using it more and more to understand why Java does what it does...after all, it's the Java bible!

like image 196
Paul Avatar answered Nov 15 '22 13:11

Paul