I have a third party library class which I cannot change, and this has a protected
instance variable which I wont to set to a non-default value on instantiating. This class has no setter nor a constructor that would allow me to set this instance variable.
I tried
// The library class I cannot change:
public class LibraryClass {
protected boolean instanceVar = false;
}
// My code:
public class MyClass {
LibraryClass myInstance = new LibraryClass() {
instanceVar = true;
};
}
but got the compiler error "<identifier> expected" on the line instanceVar = true;
. I also tried to preceed this line by this.
and super.
, but got the same error message.
Of course, I can create a non-anonymous descendant class and set the variable in its constructor. But is there a possibility to initialize the ancestor instance variable directly in an anonymous class?