0

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?

3

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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 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.
Thanks for the reply....If it got initialized without constructor. then could you please tell me what is the purpose of constructor ?
@Hari The constructors let you to assign non-default values to the instance variables.
The purpose of constructor is, to preparing the instance variable to initialize by user defined values. Am I right?
|
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.

1 Comment

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?
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.