Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do instance variables have default values in java? [duplicate]

Tags:

Why do variables declared in a class have default values, but the variables declared inside methods, said to be "local variables", don't have default values in Java?

For example

class abc {    int a;     public static void main(String ss[])    {         int b;            abc aa=new abc();           System.out.println(aa.a);           System.out.println(b);     }  } 

In this above example variable a has default value of 0 but variable b gives error that it might not have been initialized.

like image 551
Shikhil Bhalla Avatar asked Aug 14 '13 08:08

Shikhil Bhalla


People also ask

Do instance variables have default value in Java?

Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. Values can be assigned during the declaration or within the constructor. Instance variables can be accessed directly by calling the variable name inside the class.

Why do member variables have default values whereas local variables don't have any default value?

Why do member variables have default values whereas local variables don't have any default value ? Ans. member variable are loaded into heap, so they are initialized with default values when an instance of a class is created. In case of local variables, they are stored in stack until they are being used.

Are instance variables initialized to a default value yes or no?

You don't have to initialize instance variables, because they always have a default value. Number primitives (including char) get 0, booleans get false, and object reference variables get null.

What is the default value for an instance variable of type String?

An instance variable can also be a variable of object type. For such variables, the default initial value is null. (In particular, since Strings are objects, the default initial value for String variables is null.)


2 Answers

All member variable have to load into heap so they have to initialized with default values when an instance of class is created. In case of local variables, they don't get loaded into heap they are stored in stack until they are being used before java 7, so we need to explicitly initialize them. Now the "Java Hotspot Server Compiler" performs "escape analysis" and decides to allocate some variables on the stack instead of the heap.

like image 54
Ashwani Avatar answered Oct 06 '22 01:10

Ashwani


Local variables Initialization

Variables declared in methods and in blocks are called local variables. Local variable are not initialized when they are created at method invocation. Therefore, a local variable must be initialized explicitly before being used. Otherwise the compiler will flag it as error when the containing method or block is executed.

Example:

public class SomeClassName{  public static void main(String args[]){ int total; System.out.println("The incremented total is " + total + 3); //(1) } } 

The compiler complains that the local variable total used in println statement at (1) may not be initialized. Initializing the local variable total before usage solves the problem:

public class SomeClassName{  public static void main(String args[]){ int total = 45; //Local variable initialized with value 45 System.out.println("The incremented total is " + total+ 3); //(1) } } 

Fields initialization

If no initialization is provided for an instance or static variable, either when declared or in an initializer block, then it is implicitly initialized with the default value of its type. An instance variable is initialized with the default value of its type each time the class is instantiated, that is for every object created from the class. A static variable is initialized with the default value of its type when the class is first loaded.

like image 27
Dileep Avatar answered Oct 06 '22 00:10

Dileep