user2677655
user2677655

Reputation: 1

Initialization of Instance Variables in Java

How does instance variable id gets initialized to 0 when we have provided our own default constructor and did not initialize id in it? The output comes to be id:0 status:B How is id 0?

`class Demo{
    private int id;
    private char status; 

    public Demo(){ 
    status = 'B';
    }

    public void display(){
    System.out.println("Id:="+id+" Status:"+status);
    }

    public static void main(String args[]){
    Demo ob = new Demo();
    ob.display();
    }
}`

Upvotes: 0

Views: 1800

Answers (10)

Nicholas Reid
Nicholas Reid

Reputation: 31

TL;DR - Instance variables have default values. See [1].


You are correct to state that the value is zero, or rather 0. This is because in Java, instance variables, which are variables that reside within a class, but not a method, do not have to be manually initialised.

In Java, when declaring instance variable(s), if no value is given, the compiler will apply a default value to allow the program to run. While this can be used in circumstances in which you may desire to change the value of the instance variable later, it is generally considered bad practice.

See here, the Java Documentation:

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.

For information regarding the default values as per data type, see[1].

The documentation has further information regarding other variables, namely local:

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.

The reason for the difference between local, and instance variables (in the way they are treated by the JVM) is namely that the instance variables are loaded into an area of memory known as the 'Heap' [2], and the local variables are loaded into a separate memory area called the 'Stack' [2]; although, now, there are exceptions to this [3]. The Heap stores objects and references to objects, and as such, Instance Variables are stored with their objects. Ultimately, there was a choice made, that was rather arbitrary, that forced the initialisation of local, but not instance variables.

[1] https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

[2] https://www.guru99.com/java-stack-heap.html

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

Upvotes: 1

SpringLearner
SpringLearner

Reputation: 13854

As per your code, id is an instance variable and if the instance variables are not defined then it takes up the default value. In your case as id is int so the value is 0

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122026

Below are the default intializations

The following chart summarizes the default values for the above data types.

Data Type              |  Default Value (for fields)
-----------------------+-----------------------------
byte                   |   0
short                  |   0
int                    |   0
long                   |   0L
float                  |   0.0f
double                 |   0.0d
char                   |   '\u0000'
String (or any object) |   null
boolean                |   false

Upvotes: 2

Oded Peer
Oded Peer

Reputation: 2437

"For type int, the default value is zero, that is, 0".
You can see the default values in the language specification: http://docs.oracle.com/javase/specs/jls/se5.0/html/typesValues.html#96595

Upvotes: 0

user235273
user235273

Reputation:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

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.

+------------------------+----------------------------+
| Data Type              | Default Value (for fields) |
+------------------------+----------------------------+
| byte                   |               0            |
| short                  |               0            |
| int                    |               0            |
| long                   |              0L            |
| float                  |            0.0f            |
| double                 |            0.0d            |
| char                   |        '\u0000'            |
| String (or any object) |            null            |
| boolean                |           false            |
+------------------------+----------------------------+

Upvotes: 0

Antti Kolehmainen
Antti Kolehmainen

Reputation: 1091

Primitive types default to certain values. For int it's 0.

Upvotes: 1

Lake
Lake

Reputation: 4092

In Java, every variable not initialized gets automatically initialized to the default value of the type it is declared, i.e:

  • float: 0.0f
  • int/short/byte: 0
  • long: 0L
  • Object: null
  • boolean: false

Upvotes: 1

Prasad Kharkar
Prasad Kharkar

Reputation: 13566

  • because the default value of int type is 0.
  • All instance variables are assigned with default values

Upvotes: 0

christopher
christopher

Reputation: 27356

id is a primitive type, int, which defaults to 0.

You're thinking of objects

Which are actually references to objects, and these default at null, meaning they point to no object.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Instance variables are initialized with default values, for integers it is 0.

Upvotes: 0

Related Questions