Home > Blockchain >  There is two variable 'a' and 'b' which is not initialised. 'a' variab
There is two variable 'a' and 'b' which is not initialised. 'a' variab

Time:09-06

There are two variables, a and b, which are not initialized. a variable is of class Example1 and b variable of class Main. As we know if we not initialized any variable in java then it take default value. But when I want to print variable of Main class it show error of initialization. But when I call the Example1 class variable then it executed successfully and gives default value.

What is the exact reason behind it?

class Example1 {
    int a;
}
public class Main
{
    public static void main(String[] args) {
        int b;
        Example1 e = new Example1();
        System.out.println(e.a);        // output: 0
        System.out.println(b);          // error: variable a might not have been initialized
        
    }
}

CodePudding user response:

When you write:

int b; // this is declaration

int b; is a local variable. Method local variables need to be initialized before using them. And this is why you've got an error.

When you write

 int b = 0; // this is initialization

When you write:

  Example1 e = new Example1();  // This is an initialization

Primitive default value is zero when declared as class members.

CodePudding user response:

I think, java's documentation already explains why.

Here:Definite Assignment

For every access of a local variable or blank final field x, x must be definitely assigned before the access, or a compile-time error occurs.

And here:Initial Values of Variables

Every variable in a program must have a value before its value is used:

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

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.

CodePudding user response:

as we know if we not initialised any variable in java then it take default value

This is true for class member variables but not for local variables. Refer to Default initialization in java

a is a class member variable but b is a local variable.

In fact your code does not compile. Nonetheless, my IDE (which is Eclipse) still allows me to run code that does not compile. When I run the code, I get this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The local variable b may not have been initialized

You need to explicitly initialize local variables.

Consider the following code:

class Example1 {
    int a;
}

public class Main {

    public static void main(String[] args) {
        int b = -1;
        Example1 e = new Example1();
        System.out.println(e.a);
        System.out.println(b);
    }    
}

CodePudding user response:

Class fields are initialized with a default. One cannot leave them uninitialized and let the compiler decide that upon the first usage the field is filled. Also unneeded initialization would be required. Certainly this was also influenced by C 's possible uninitialized fields.

Local variables serve an immediate purpose, and can be tracked to their first usage. It was decided that a local variable requires an explicit initialization as a default initialization could hide a forgotten explicit initialization, like assigning only in an if or try-catch statement.

  • Related