About Question enthuware.ocajp.i.v7.2.1082 :
Moderator: admin
-
- Posts: 1
- Joined: Tue Mar 22, 2016 3:37 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1082 :
I have an error compilation in IntelliJ IDE.
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1082 :
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.
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.
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1082 :
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.
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.
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1082 :
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.
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.
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1082 :
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?
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1082 :
Correct.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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Wed May 31, 2017 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1082 :
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.
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.
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1082 :
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.
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.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1082 :
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 ...]
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 ...]
Who is online
Users browsing this forum: No registered users and 89 guests