Page 1 of 1
invalid answer enthuware.ocajp.i.v7.2.1022
Posted: Sat Aug 01, 2015 6:47 pm
by jayallenloew
RE: enthuware.ocajp.i.v7.2.1022
Don't ever -- ever -- use a Java keyword as your variable name. This is an incredibly foolish thing to do.
Don't ever do this in your code regardless of what this exam says or what the compiler allows.
Re: invalid answer enthuware.ocajp.i.v7.2.1022
Posted: Sat Aug 01, 2015 8:48 pm
by admin
While what you are saying is indeed correct advice, please note that String is not a Java keyword.
Having said that, the given question and the answer are not invalid. The given code is perfectly legal.
HTH,
Paul.
Re: invalid answer enthuware.ocajp.i.v7.2.1022
Posted: Sat Aug 01, 2015 9:31 pm
by jayallenloew
Well alright, technically speaking, false, true, and null are not listed on the Oracle site as keywords either.
But we are expected to realize they must never be used as the names of our variables in the Java language.
Frankly, this is like a man saying he figured out a way to drive ninety through a school zone and not get stopped. It's just something that should never be attempted.
Re: invalid answer enthuware.ocajp.i.v7.2.1022
Posted: Sat Aug 01, 2015 9:41 pm
by admin
jayallenloew wrote:Well alright, technically speaking, false, true, and null are not listed on the Oracle site as keywords either.
true, false, and null are literals. String is not that either. That is the whole point of the question. The name String has no special meaning for Java language.
But we are expected to realize they must never be used as the names of our variables in the Java language.
Again, this is a valid point but not much relevant for the exam. The exam is more focused on legality of constructs and less on practical aspects.
Whether it is a good thing or a bad thing is a separate discussion.
Re: invalid answer enthuware.ocajp.i.v7.2.1022
Posted: Sun Jan 27, 2019 10:57 am
by DmitryM
Another crazy example of using a class name for a reference variable. Based on the previous question I thought it would compile.
But it would not! The reason is the right side Integer refers to the Integer
variable, not the class name. And it was not initialized in the local block.
Code: Select all
public static void main(String[] args) {
Integer Integer = Integer.parseInt("10");
}
The error is: "Uncompilable source code - variable Integer might not have been initialized"
Re: invalid answer enthuware.ocajp.i.v7.2.1022
Posted: Sun Jan 27, 2019 11:17 pm
by admin
Interesting example indeed, DmitryM!