1

Can some one time the reason behind assigning default values for the data members and not for the local variables ? Is there a specific reason for that ?

Example :

Class A {
B b;

public void f(){
int a;
}

In the above class b is initialized with null, however the compiler throws and error saying that variable a inside f() is not initialized.

Aditya
  • 532
  • 2
  • 5
  • 14

2 Answers2

2

I would say it was (as always) practical reason. You can initialize object at one point and use it some time later - then you have to have some mechanism that ensure that everything was initialized into some default value in case we cannot set them immediately.

Local object on the other hand are used immediately after they were declared - so we can safely assume programmer is able to initiate them with their target value, and it should be encouraged.

Bottom line - just practical reasons, encouraging the good practices which would prevent some errors, nothing like technical limitations.

Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64
  • See this answer by Eric Lippert, it was originally asked on the C# language but considering the similarity between the languages I think the reasons are the same. http://stackoverflow.com/questions/8931226/are-c-sharp-uninitalized-variables-dangerous/8933935#8933935 – Vitaliy Apr 20 '14 at 10:41
  • Well, I don't know exactly what's happening inside JVM during locals initialization. but it wouldn't be difficult to enforce compiler to do implicit initialization to default value. I cannot see any technical limitation that would enforce language designers to do so, so I assume it was just practical reason - it helps preventing errors. Debugger not showing garbage would also be a profit, but I'm not sure that current design of JVM would ever allow you to see garbage (unless from some JNI method but in default values that's never the case). – Mateusz Kubuszok Apr 20 '14 at 10:50
-2

Why data members are initialized with default values

When you compile a class, the Java compiler creates an instance initialization method for each constructor you declare in the source code of the class. Although the constructor is not a method, the instance initialization method is. It has a name, , a return type, void, and a set of parameters that match the parameters of the constructor from which it was generated.

If you don't explicitly declare a constructor in a class, the Java compiler will create a default constructor on the fly, then translate that default constructor into a corresponding instance initialization method. Thus, every class will have at least one instance initialization method.

Whereas local variables are not

When you allocate resources for a local variable, Java doesn't write a value into the memory. The reason you get an error is because Java makes sure you give it a value before you use it. The stack contains the local variables, so they might take the value of whatever was on the stack before them. To guard against this, the compiler can check whether they have been initialised or not.

Hello World
  • 944
  • 2
  • 15
  • 39