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?