This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I got this question taking Jxam mock Exam: Choose all valid variable names: 1) _int 2) %bob 3) $str 4) Integer 5) 2num I put (1) and (3), but it also said (4) was correct. Now I'm sure I'm sounding dumb asking this, but I thought that Integer was already a "taken" name. Integer i = new Integer(); int i = Integer.parseInt("32"); If I'm wrong, it won't be the first time, but I'd like to please have this clarified asap. All help is appreciated. Thanks again, Travis B.
It will compile OK, but now you have the headache of trying to work out when you are referring to the class java.lang.Integer or to your local variable called Integer. Just because legally you can do something doesn't make it good practise to do so. That is why coding guidelines are so important.
Class names are valid names for identifiers... try this:
This code compiles and runs fine... Nothing prevents you to use class names for identifiers... If you want more details, you can check out: JLS 6.2 Names and Identifiers JLS 3.8 Identifiers [ February 26, 2002: Message edited by: Valentin Crettaz ]
What mock exam has this? I don't believe this is good practice and should not be posed as good practice! -Dale
By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Joined: Aug 26, 2001
Dale, you are right this is not a good practice to code like that, but this question is only to show that such coding practices are possible. You could use such a technique in a code obfuscator for instance which would replace identifiers with completely unrelated class names. That way it would be kind of difficult to figure out what the code does... Obfuscating the code is not a secure enough technique to protect your source code, though. [ February 26, 2002: Message edited by: Valentin Crettaz ]