About Question enthuware.ocpjp.v7.2.1466 :

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

Moderator: admin

Post Reply
doziransky
Posts: 8
Joined: Wed Mar 16, 2016 10:57 am
Contact:

About Question enthuware.ocpjp.v7.2.1466 :

Post by doziransky »

So I'm having trouble understand why this works:

System.out.println(C.this.c);

I thought that since C is still being constructed (as we're in the constructor) the value of c would be null and only become 'c' after the constructor has finished running.

I tried writing this code and it's clear that c does in fact contain the value 'c' before the constructor finishes running, but why is this the case?

How does C have instance variable values before it actually exists?

Thanks for your help.

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

Re: About Question enthuware.ocpjp.v7.2.1466 :

Post by admin »

You should remember that all instance fields get their default values first, even before the constructor runs. These default values are 0 for numeric types, false for boolean, and null for reference types.

This is done by the JVM itself when it allocates memory for the object. It cleans the memory locations of these fields and then gives the control to the instance initializers and then the constructor so that they can initialize the value of these fields as per the business logic.

That is why, the field has a value even before the constructor is finished.

BTW, why do you think that c would be null? Primitives types can never be null. They are not references. They are primitives. They don't point to anything. They just have a value, which is 0 to begin with (or false for boolean.)
If you like our products and services, please help us by posting your review here.

doziransky
Posts: 8
Joined: Wed Mar 16, 2016 10:57 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1466 :

Post by doziransky »

Oh woops yea I meant the default value, not null. Was thinking from an object perspective.

And okay cool I had no idea it worked like that, thanks!

vahankh
Posts: 3
Joined: Mon Aug 13, 2018 2:21 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1466 :

Post by vahankh »

Why

Code: Select all

         System.out.println(B.this.c);
prints a instead of b?

Isn't B already constructed?

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

Re: About Question enthuware.ocpjp.v7.2.1466 :

Post by admin »

Because B.this.c refers to the "char c = 'a'" definition in class B. This c is not being modified anywhere.
If you like our products and services, please help us by posting your review here.

vahankh
Posts: 3
Joined: Mon Aug 13, 2018 2:21 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1466 :

Post by vahankh »

admin wrote:
Mon Aug 13, 2018 8:23 pm
Because B.this.c refers to the "char c = 'a'" definition in class B. This c is not being modified anywhere.
Thanks. But what about the following constructor?

Code: Select all

B() { super('b'); }
Doesn't it change the value of 'a'?

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

Re: About Question enthuware.ocpjp.v7.2.1466 :

Post by admin »

What do you mean by changing the value of 'a'? 'a' is a value. c is the variable that can be changed. However, why do you think super('b') changes the instance variable c of class B?
If you like our products and services, please help us by posting your review here.

vahankh
Posts: 3
Joined: Mon Aug 13, 2018 2:21 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1466 :

Post by vahankh »

When parent constructor is called

Code: Select all

A(char c) { this.c = c; }
Doesn't this refer to object B?

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

Re: About Question enthuware.ocpjp.v7.2.1466 :

Post by admin »

No, it refers to A's c.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests