Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1116 :

Posted: Tue Feb 19, 2013 6:44 am
by ETS User
Hi all, I don't get why isn't correct the answer B and D, the instance - static member of a class aren't initialized by default constructor?
How can you see the definition of the default constructor if it isn't defined in the code? Should I decompile the class file?

Best regards

Re: About Question enthuware.ocajp.i.v7.2.1116 :

Posted: Tue Feb 19, 2013 9:03 pm
by admin
Yes, you can decompile the byte code and see that the default constructor exists and what it does. Actually, it does nothing.

It is basically same as:

ClassName(){
}

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1116 :

Posted: Thu Feb 28, 2013 9:50 am
by muttley
In my opinion the answer:
It initializes the instance members of the class.
It's correct.
The default constructor (and all kind of constructor) initialize a instance member.
Right?

Re: About Question enthuware.ocajp.i.v7.2.1116 :

Posted: Fri Mar 01, 2013 7:05 am
by admin
No, it is not correct. The default constructor provided by the compiler does not initialize any thing. You can write your own constructor to initialize the members.

Re: About Question enthuware.ocajp.i.v7.2.1116 :

Posted: Tue May 21, 2013 1:53 am
by berzerk
I can't see why muttley is wrong - doesn't the default constructor initialise the instance variables of the class to their default values - thus B being correct?

If the default constructor doesn't initialise uninitialised variables to their default values, then what does?

kind regards - real exam in 3 days... :D

Re: About Question enthuware.ocajp.i.v7.2.1116 :

Posted: Tue May 21, 2013 6:46 am
by admin
The default values are provided even before the constructor is executed. These values are given when the memory is initialized for the object. The following code proves that the default value is given before the constructor is executed:

Code: Select all


class A {
    int i = 10;
    public A() {
        System.out.println("In A's Constructor, A's i = " + i);
        printValue();
    }

    //This is not invoked for an object of class B
    public void printValue() {
        System.out.println("In A's printValue : A's i " + i);
    }
}

public class B extends A {
    int i = 20;
    public B() {
        i = 30; //B's constructor sets i to 30
        System.out.println("In B's Constructor, B's i = " + i);
    }
    
    
    //override A's printValue
    //this is called from A's constructor even before B's constructor is invoked. 
    //Prints 0 because B's constructor hasn't even been called yet so i still has its default value assigned by the JVM
    public void printValue() {
        System.out.println("In B's printValue B's i =  " + i);
    }

    public static void main(String[] args) {
        B b = new B();
    }
}
Output:

Code: Select all


In A's Constructor, A's i = 10
In B's printValue B's i =  0
In B's Constructor, B's i = 30
HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1116 :

Posted: Wed Dec 23, 2015 11:38 pm
by TomorrowWorld
The easiest way to look at this question would be to recall the order of initialization of blocks of an class.

1) static variables and static blocks according to the order they appear on the class
2) instance variables and instance blocks according to the order they appear on the class
3) constructor

So, instance variable have already been initialized to their defaults before constructor get calls.
Answer B and D are incorrect. Because we could re-initialize instance fields, but that is not constructor's responsibility always.