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

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

Moderator: admin

Post Reply
andusci
Posts: 1
Joined: Tue Mar 22, 2016 3:37 pm
Contact:

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

Post by andusci »

I have an error compilation in IntelliJ IDE.

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

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

Post by admin »

1. While preparing for the exam, please use command line tools instead of an IDE.
2. What is the error? Usually the error message is quite clear about what's wrong.
Paul.
If you like our products and services, please help us by posting your review here.

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

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

Post by jamesmccreary »

I initially changed my answer from the correct one. I realize that the concept being tested here is local member shadowing, but the parentheses expression is the first to evaluate. However, given that we are now in local scope, x has not been defined. The only explanation I can think of is that x was declared somewhere (in fact it was initialized too, on the instance level), but ignores the initialized value, "keeps" the declaration and then the compiler realizes that shadowing is taking place with the same variable name.

That sounds a bit too complicated to be correct. Why can x= (some value) be valid? What if I wanted x to be a String or an Object? Does the compiler automatically assign the declared type of the variable to be the same as the value you are assigning it (3 is an int, so therefore I, the compiler will make myself happy and help the programmer out and implicitly declare the x inside the parentheses as type int)?

Pardon my confusion over a potentially obvious triviality.

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

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

Post by admin »

You can comment out the static int x declaration and it will still work.
So the point is x is considered declared on the right hand side of = in the statement int x = ( x=3 ) * 4; Even inside the parentheses.

Regarding the type of the values - You cannot assign just anything to a variable. The type of the value has to be compatible with the type of the variable. If you want to assign a String or an object, you cannot do that if the type of x is an int. There is no implicit declaration of any kind here.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

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

Post by jamesmccreary »

Thank you Paul. To be clear, the left-hand side "int x" declares all subsequent x's in the same scope (e.g. the x in the parentheses is considered declared due to the left-hand side x?

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

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

Post by admin »

jamesmccreary wrote:Thank you Paul. To be clear, the left-hand side "int x" declares all subsequent x's in the same scope (e.g. the x in the parentheses is considered declared due to the left-hand side x?
Correct.
If you like our products and services, please help us by posting your review here.

nveleven22
Posts: 1
Joined: Wed May 31, 2017 6:24 pm
Contact:

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

Post by nveleven22 »

Tiny nitpick - The local 'x' simply shadows the member variable 'x'.

I'd argue the programming term "shadow" is at risk of misinterpreted by the wording of this explanation due to the ambiguous nature of the the word "shadow" as a verb can mean both to "follow behind" or to "be overshadowed" depending on how it is conjugated.

A proper usage of the word shadow could read:

The intern shadows his superiors.

Where the lesser important subject "shadows". Extrapolated to the current explanation, one could mistakenly infer the local 'x' is the less important subject.

Wikipedia claims that in programming, the local variable would mask the member variable while the member variable is shadowed, perhaps that terminology would be of value here.

EDITED: for clarity.

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

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

Post by admin »

Hi,
While you may be correct about the English language meaning of the word "shadow" but it is a technical term and programming in general and Java in particular has a specific meaning of this word. It is explained here: https://docs.oracle.com/javase/specs/jl ... #jls-6.4.1

In this case, it is correct to say that the local variable shadows the instance variable.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

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

Post by JuergGogo »

Another interesting detail in this example:
a static member variable can be shadowed by a local (non-static) variable. However a static method can't be hidden by a non-static method.

Anyway I'm not sure wether shadowing (local variables?) and hiding (members of subclasses?) is the same thing.

Edit: I should have read the above link --> Java Language Description: [ ... Shadowing is distinct from hiding ...]

Post Reply

Who is online

Users browsing this forum: No registered users and 89 guests