1

we know that...

Instance Variable are initialized in default constructor. For eg.

public class H{
    int x;

    public static void main(String... args){
        System.out.print(new H().x);
    }
}

The O/P of above code is 0 because there is a default constructor which is called , and that constructor initialized the x to 0.

Now, my question is, if we run the below code, i.e.

public class H{
        int x;

        public H(){}

        public static void main(String... args){
            System.out.print(new H().x);
        }
}

The actual O/P is 0 in this case also, but I think there should be compiler error that x is not initialized, because we have override the default constructor and didn't initialize x.I think I have made my question clear..

3
  • 2
    instance variables are initilized to default values according to their types. Commented Aug 29, 2014 at 11:48
  • but that initialization is done in default constructor,, my question is in the second code,, call to default constructor will never occur.. Commented Aug 29, 2014 at 11:49
  • 1
    Field initialization happens before constructor invocation. Commented Aug 29, 2014 at 11:51

5 Answers 5

8

In Java, instance members are defaulted to the all-bits-off version of their value automatically (ints are 0, object references are null, floats are 0.0, booleans are false, and so on). It's not something the default constructor does, it's done before the constructor runs.

The order is:

  1. Default the instance members to their all-bits-off value. (The optimizer can skip this if it sees #2 below or possibly if it can prove to itself that nothing uses the member prior to an initialization per #3 below.)

  2. Apply any inline initialization of them. For instance:

    int a = 42;
    
  3. Apply instance initialization blocks in source code order.

  4. Call the appropriate constructor.

So for example:

class Example {

    int a = 42;

    // Instance initializer block:
    {
        this.a = 67;
    }

    Example() {
        System.out.println(this.a);
    }
}

new Example() outputs 67.

Obviously, initializing in both places like that would be poor practice, this is just for illustration.

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

Comments

8

Non-final fields are initialized by default in java. Only variables inside methods and final fields are not initialized by default.

If you had declared x to be final, then you would be correct. You would have a compile error in the code.

Comments

3

All instance level variables are initialized to their default values irrespective of whether the constructor has been overloaded ( or explicit no-argument constructor has been added). The constructor merely changes the default value(s).

Comments

2

Instance variables have default values associated with them

From The Java™ Tutorials:

Default values

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type.

5 Comments

Are they really initialized by the compiler? Are they not simply initialized by the jvm at runtime? Because if you are right I should see explicit field initializations in the bytecodes generated by the compiler and I doubt that's the case.
well this document says they are initialized by the compiler
Interesting. It looks like you are completely right. They might be store in initialization tables in the class file and not directly in bytecodes. I would have thought different. +1 for the lesson.
@EdwinDalorzo: I think the document is just being a bit loose with its terminology there. As you say, there's nothing in the bytecode initializing the instance members, and it's the kind of job that seems like it's more the JVM's responsibility than the compiler's. Not that it really matters...
@T.J.Crowder I would need to read the JVM spec again, but I think what could be happening is that the compiler sets the initialized values in some kind of initialization table and later JVM reads the values and initializes the fields. So, ultimately it has to be the JVM which does the work at runtime, that's for sure. By decompiling the source it does not happen with explicit bytecodes though, that's why I think they are probably written by the compiler in some kind of table in the binary class file.
1

Java is quite neat to programmers (unlike others, C for instance), this means that it initializes fields automatically. An exception is final fields and fields inside a methods (where the compiler will then produce an error).

Hope it helped.

2 Comments

What does it mean "fields inside methods"?. You know there are several other answers already. You should elaborate more in your thoughts and offer examples and different perspectives to the answer, above all if you want more upvotes. I would consider upvoting you if you elaborate more.
Thanks a lot for your feedback. Since a very good and illustrated answer as already been given I won't edit mine, but I take not for a future answer I could give!

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.