One of my kids is taking Java in high school and had this on one of his tests:
Which of the following is a valid identifier in Java?
a.
123java
b.main
c.java1234
d.{abce
e.)whoot
He answered b and got it wrong.
I looked at the question and argued that main
is a valid identifier and that it should have been right.
We took a look at the Java spec for identifiers and it reinforced that point. We also wrote a sample program that had a variable called main
, as well as a method. He created a written rebuttal that included the Java documentation reference, the test program and the teacher ignored it and says the answer is still incorrect.
Is main
a valid identifier?
This compiles, and when executed, emits this output:
The character sequence
main
is an identifier, not a keyword or reserved word.The relevant section of the JLS is 3.8:
The character sequence
main
fits the above description and is not in the keyword list in Section 3.9.(The character sequence
java1234
is also an identifier, for the same reasons.)This compiles fine on Java 1.8...
...and when executed produces the output:
I threw everything I could at it, and it appears to work. I'd say main is a valid identifier.
Is it a valid identifier? Yes.
Is it a good identifier? Not if you're using it for anything other than the method that starts at JVM launch.
Is another valid identifier listed? Yes.
Did the test instructions say to choose the best answer?
That teacher made a minor mistake in either assuming main is not a valid identifier or simply phrasing the question wrong. He possibly meant to say "a good identifier".
But ignoring your sons arguments and thereby discouraging his scientific approach of checking relevant literature (Java specification) and performing an experiment (writing a sample program) is the exact opposite of what a teacher is supposed to do.