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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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

admin
Site Admin
Posts: 8920
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

muttley
Posts: 19
Joined: Thu Feb 28, 2013 9:47 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 8920
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

berzerk
Posts: 4
Joined: Tue Apr 30, 2013 8:48 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 8920
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

TomorrowWorld
Posts: 2
Joined: Wed Dec 23, 2015 11:27 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests