I have done a lot of searching for what this compiler error <identifier> expected
means, and none of them seem to apply to my situation. Really sorry if this is a duplicate or basic info, but I can't find anything anywhere.
The following code works fine. Note that I am positive myObject1
is indeed a HashSet<String>
so the cast is ok.
@SuppressWarnings("unchecked")
HashSet<String> s1 = (HashSet<String>) myObject1;
The following code does NOT work fine. It will compile, but with warnings.
@SuppressWarnings("unchecked")
HashSet<String> s1;
s1 = (HashSet<String>) myObject1;
So then I try the code below.
@SuppressWarnings("unchecked")
HashSet<String> s1;
@SuppressWarnings("unchecked")
s1 = (HashSet<String>) myObject1;
Now it refuses to even compile, giving me the <identifier> expected
error that is puzzling me so much. The ^
symbol in my command line is pointing right before the =
in the last line. I'm not sure what on earth I could be expected to put BETWEEN the s1
and the =
.
Any ideas? Thanks!
You can't apply an annotation to a simple assignment statement. From section 9.7 of the JLS:
Annotations may be used as modifiers in any declaration, whether package (§7.4.1), class (§8.1.1) (including enums (§8.9)), interface (§9.1.1) (including annotation types (§9.6)), field (§8.3.1, §9.3), method (§8.4.3, §9.4), formal parameter (§8.4.1), constructor (§8.8.3), or local variable (§14.4.1).
I agree that the compiler error message could be rather clearer, admittedly...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With