I am new to java and I have a doubt about object initialization.
What I currently know:
Constructors are used to initialize the instance variables and if we don't explicitly code the constructor, default constructor is provided that automatically provides the default values to the instance variables like 0 for int, etc.
My Question: How did the following code work(I didn't initialized the instance variable)?
I tried a basic code as follows:
public class hello{
int i; //Instance variable
public hello()
{
//Constructor is empty!!!
}
public static void main(String args[])
{
System.out.println(new hello().i);
}
}
And the result was 0, but how? I didn't did anything in the constructor and since I explicitly coded the constructor the default constructor shouldn't be invoked(I know I am having a wrong concept in my mind, so please correct me).
How did the above code worked, please clear my doubt. Thank You!