Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1082 :
Posted: Tue Mar 22, 2016 3:39 pm
by andusci
I have an error compilation in IntelliJ IDE.
Re: About Question enthuware.ocajp.i.v7.2.1082 :
Posted: Tue Mar 22, 2016 7:39 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1082 :
Posted: Mon Feb 13, 2017 5:38 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1082 :
Posted: Mon Feb 13, 2017 9:28 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1082 :
Posted: Wed Feb 15, 2017 12:41 pm
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?
Re: About Question enthuware.ocajp.i.v7.2.1082 :
Posted: Wed Feb 15, 2017 8:53 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1082 :
Posted: Wed May 31, 2017 6:37 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1082 :
Posted: Wed May 31, 2017 10:10 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1082 :
Posted: Thu Oct 05, 2017 9:24 am
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 ...]