4

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!

3 Answers 3

8

The default constructor does not initialize anything. It is empty. Field level variables are automatically initialized to some default values (like 0 for int types) regardless of constructor.

Other default values are

boolean -> false
double -> 0.0D
[any object reference incl. String] -> null
Sign up to request clarification or add additional context in comments.

5 Comments

"The default constructor does not initialize anything." That's not actually true at a bytecode level if you provide initial values for fields (e.g.,int field = 5;), but it's fine conceptually... (The compiler inserts those assignments into all constructors you define.) But yes, it's the process of creating the instance, not the constructor, that sets the default values.
@T.J.Crowder Interesting, I didn't know that. Thanks for sharing
Thank you for your replies, but I am now even more confused? Please help me!. The confusion is in C++ when you don't code the constructor and don't initialize the instance variable if you display it, you will get garbage value as it was not initialize. So, from there I picked the concept that constructors initialize the instance variables but now you are saying that they are initialized as soon as the instance is created, so why am I getting garbage value in C++ then? Is this about the difference between JAVA and C++, Please reply??
@KaushalJain: Yes, this is a difference between C++ and Java. There are many, and this is one of them. In Java, instance fields that you don't explicitly initialize are guaranteed to have their "all bits off" default value; this is guaranteed by JLS §4.12.5 and JLS §15.9.4.
Thank you for the links T.J. Crowder, they arereally helpful!
5

You might be confusing instance variables and local variables. Local variables are the ones that must be initialized before use, otherwise you get a compile error. int i in this case is an instance variable, which can be left uninitialized without causing the compiler to complain. int instance variables are 0 by default.

Comments

2

If you provide no explicit initialization to instance variables, they will be awarded predictable default initial values, which are based only on the type of the variable.

Here are the default value of all types:

Type                          Default Value
boolean                          false
byte                            (byte) 0
short                           (short) 0
int                                 0
long                                0L
char                             \u0000
float                              0.0f
double                             0.0d
object reference                   null

Even though You have explicitly created default constructor with empty code in it

In your code when below line will get executed

System.out.println(new hello().i);

If you don't explicitly initialize an instance variable in your constructor, that variable will retain its default initial value when new returns its object reference.At that point of time, Your instance variable is set to its default value by JVM.

And also whenever object needs to be created, JVM will call Instance Initializer Block[IIB] before calling default constructor.

Thanks

2 Comments

Yes!!!!!!!!!!!!! Thank you for your reply, you cleared my concepts from top to bottom!. You mean to say that first the instance variables are initialized by the JVM then the focus goes to the constructor to assign values(if they are coded), and if the values are not assigned they retain their intial values! That was your point right? Thank you so much you cleared my concepts!!!!! God Bless You!!! I was stuck on this one for about an hour!!!
@KaushalJain, Yes Exactly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.