-1

I understand that, Constructors are used to initialize the instance variables. Default constructor will create by compiler itself, if we did not create the same. If we are creating the parameterized constructor then compiler won't create the default constructor. I have written a code, to ignore the instance variable to be handled by constructor. But it got initialize without constructor. How it come possible to initialize variable without the constructor? Please find the code snippet below for better understanding

public class ClassWithoutDefault {
int number;
String name;

//Intailizing name variable alone by using parameterized constructor
ClassWithoutDefault(String name){
    this.name = name
}
void show(){
    System.out.println("Name is"+name+"Number is"+number );

}
}

//Main class
public class ConstructorTest {

public static void main(String[] args) {
    ClassWithoutDefault classWithoutDefault = new ClassWithoutDefault("Hari");
    classWithoutDefault.show();
}

}

Output

Name is Hari
Number is 0

How the variable number got initialized as 0, without the constructor?. Could any one please help me to understand this?

Hari
  • 97
  • 1
  • 14

3 Answers3

3

Each member variable is assigned a default value (when an instance of a class is created), and if you don't assign it anything else, it retains that default value. The default value of primitive numeric types is 0.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thanks for the reply. As per my understanding, setting the variable to default value is called initialization and it will be done by constructor. please correct me if I am wrong. In code I have skipped, the variable to be handle by constructor. then how it is set as 0, even it is not handled by constructor? – Hari Jun 07 '16 at 11:54
  • 1
    @Hari No, the initialization of instance variables to their default values is performed before your constructor is executed. If it was possible to skip the assignment of default values, it would be a compilation error to access an instance variable that may have not been initialized yet. – Eran Jun 07 '16 at 11:56
  • Thanks for the reply....If it got initialized without constructor. then could you please tell me what is the purpose of constructor ? – Hari Jun 07 '16 at 11:59
  • 1
    @Hari The constructors let you to assign non-default values to the instance variables. – Eran Jun 07 '16 at 12:01
  • The purpose of constructor is, to preparing the instance variable to initialize by user defined values. Am I right? – Hari Jun 07 '16 at 12:06
  • @Hari Try to think of it this way: it is `new` keyword which actually creates instance of class (code from constructor is executed after it), but problem is that this instance returned by `new` is not properly initialized (its fields are set to default values: `0`, `0.0` `false` `'\0'`). So purpose of constructor is not to *prepare* instance to initialize, but to actually initialize that instance properly. So in constructor you can set specific fields to values different than default ones. You can also add some optional code like checking if new values *make sense*. – Pshemo Jun 07 '16 at 12:24
  • @Eran: Sorry this is not related to answer but I found quite a few of your old posts deleted and I am voting to undelete them. Please [check this meta post](https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled/10844#10844) that says dupe question should NOT be deleted. – anubhava Feb 21 '21 at 06:48
1

Whenever a new object of a class is created, :

  • Strings are initialized by null.
  • Numbers are initialized by 0 (integers), or 0.0(floating-point).
  • booleans are initialized by false.
  • chars are initialized by \u0000
  • Arrays are initialized by the default values of their components.
  • Other Objects are initialized by null.

Hence, your number was initialized by 0.

dryairship
  • 6,022
  • 4
  • 28
  • 54
  • Thanks for the reply. As per my understanding, initialization of variable is done by constructor, when the time of object creation. please correct me if I am wrong. In code I have skipped, the variable to be handle by constructor. then how it is set as 0, even it is not handled by constructor? – Hari Jun 07 '16 at 11:57
0

When you create an object,first compiler copy that class file to memory and create the template of the object in the memory. And then it creates a copy according to that template and add it to your reference variable. If you declared a static variable then that variable is created with the template,thats why static variables have the last updated value everytime. Because for all objects there is only one common variable in that name in the memory. But when you create instance variable it is made with every object. So when an object is created everytime, it is initialized to its Default Value.

//byte  0
//short 0
//int   0
//long  0
//float 0.0
//double 0.0
//char null
//boolean false
//Reference Type, class type - null
Janith Ganewatta
  • 143
  • 1
  • 4
  • 12