I have an abstract
superclass that has a protected String
variable. It is not initialized in the superclass.
All I want to do is initialize and use it in a subclass. So, I have something like this:
public class MySubclass extends MyAbstractSuperclass {
thing = "i'm a thing";
[...]
}
Seems simple enough, since the whole point of inheritance is reusing classes and their fields in different contexts. But that is wrong: Syntax error on token "thing", VariableDeclaratorId expected after this token
.
I can get it to compile by putting brackets around the subclass initialization. What I would like to know is why the first way is wrong, and why brackets fix it. Thanks for any help.
edit: I appreciate the tip that I can fix this by using a constructor. However, I am still curious as to what is actually wrong with using a superclass field in this way, as it seems to me to be pretty intuitive. And also, why do the braces fix it, what exactly does it mean to surround a statement with braces like that outside of e.g. a loop structure?