[HD Pg 73, Sec. 3.4.1 - accessing-object-fields]

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

Moderator: admin

Post Reply
Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

[HD Pg 73, Sec. 3.4.1 - accessing-object-fields]

Post by Username987654 »

Since the method call is chained directly to the object creation, the **compiler** is able to create a temporary reference variable pointing to the newly created object and invoke the method using that variable. However, this variable is not visible to the programmer and therefore, after this line, we have lost the reference to the Calculator object and there is no way we can access the same Calculator object again.

Within the calculate method, the same Calculator object is available though, through a special variable called "this", which is the topic of the next section. Similarly, the **compiler** created a temporary reference variable for the array object and passed
it in the method call.
The compiler does not execute any code and it has no knowledge of the values that a variable might take during the execution of the program.
Should **compiler** be JVM?

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

Re: [HD Pg 73, Sec. 3.4.1 - accessing-object-fields]

Post by admin »

No, it is fine. The variables are created (i.e. put in the bytecode) by the compiler not the JVM.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 73, Sec. 3.4.1 - accessing-object-fields]

Post by Username987654 »

I get it that 1) this section is literally about accessing object fields, and 2) books have to be read in their entirety, otherwise the result is something that is far far less approachable (I think) ....like the JLS (my opinion, granted). But, is there more room for clarification allowing for us readers another opportunity to properly disambiguate compilation versus run time potential issues ahead of exam time?

The compiler invokes methods using variables? I thought that the JVM was responsible for invocations? The (unmentioned here) JVM creates the newly created object? That object does not exist at the time of compilation?

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

Re: [HD Pg 73, Sec. 3.4.1 - accessing-object-fields]

Post by admin »

I am trying to understand the cause of confusion but am not sure if I do.

Here, "invoke the method using that variable" really means "The compiler generates the bytecode that instructs the JVM to invoke the method using that variable" because as you have mentioned, compiler does not execute code, JVM does.

Similarly, "compiler creates the object upon hitting the new keyword" really means "compiler creates the bytecode that instructs the JVM to create an object".

Since it is obvious that compiler does not execute any code, I am not sure if putting the phrase, "generates the bytecode that instructs the JVM to invoke" into such statements would make the statements any more clear than just saying "The compiler invokes the method using that variable" or "the compiler creates an object". It would just be an unnecessary distraction because the meaning is quite clear here already.

It is like when you say, "you create new object using the new keyword", you really mean, "you request the compiler to generate the bytecode that instructs the JVM to create a new object using the new keyword" because, yes, you do not create any objects, neither does the compiler. The JVM creates them.

Another way to look at it would be this - the compiler "tries" to invoke a method using a variable but, since the JVM is the one that actually invokes the method at run time, it decides which method to actually invoke (this is what happens with instance methods.

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 73, Sec. 3.4.1 - accessing-object-fields]

Post by Username987654 »

admin wrote:
Wed Nov 20, 2019 6:57 am
I am trying to understand the cause of confusion but am not sure if I do.
Ultimately, I just wanted to ensure that I have been absorbing the material as best I can. Judging from the response, I think I have clarity here but wondered if just the aforementioned text could have been taken incorrectly (by anyone [self included]) when read separately from other book text. However, I make absolutely no representations about how this portion of this fantastic book could be better presented. Thank you greatly for the detailed explanation.

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

Re: [HD Pg 73, Sec. 3.4.1 - accessing-object-fields]

Post by admin »

Thanks for sharing your thoughts. We will revisit this section and make it more clear.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests