In the above code, you get a compilation error on line 5 saying comma is expected instead of semicolon.
However, I was expecting the error on line 6 as I was modifying a final variable from a parent class. While I can see, if I change the code to :
There is no compilation error. Makes sense!
But the line number of the compilation error is confusing to me in the sense such kind of questions are asked in OCAJP where we are expected to know the line number where the compilation error comes.
Do you know any other example similar to this one? Thanks!
That tells you how difficult it is to write good compiler error messages. Yes, you would have expected the compiler to complain about trying to reassign a final field, but that is a statement, and statements are only permitted in methods, constructors, initialiser, etc. That is code lying free in the class, where only declarations are permitted. If it is a declaration, it hasn't got a type, so the likeliest error is that it is part of the previous declaration. It will compile asYou are declaring the same field twice in two classes, which is dubious and error‑prone design, and you are declaring two fields in the same line, which is poor style. In fact both those offences against good programming should be banned everywhere . . . except in cert. exams
posted 2 weeks ago
Many thanks for the explanation. It's much clear now.
PS: I am really amazed how quickly I get answers/explanation on this forum. A big Thank You to all of you!