About Question enthuware.ocpjp.v7.2.1466 :
Moderator: admin
-
- Posts: 8
- Joined: Wed Mar 16, 2016 10:57 am
- Contact:
About Question enthuware.ocpjp.v7.2.1466 :
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.
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.
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1466 :
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.)
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.
-
- Posts: 8
- Joined: Wed Mar 16, 2016 10:57 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1466 :
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!
And okay cool I had no idea it worked like that, thanks!
-
- Posts: 3
- Joined: Mon Aug 13, 2018 2:21 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1466 :
Why
prints a instead of b?
Isn't B already constructed?
Code: Select all
System.out.println(B.this.c);
Isn't B already constructed?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1466 :
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.
-
- Posts: 3
- Joined: Mon Aug 13, 2018 2:21 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1466 :
Thanks. But what about the following constructor?
Code: Select all
B() { super('b'); }
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1466 :
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.
-
- Posts: 3
- Joined: Mon Aug 13, 2018 2:21 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1466 :
When parent constructor is called
Doesn't this refer to object B?
Code: Select all
A(char c) { this.c = c; }
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1466 :
No, it refers to A's c.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 64 guests