Simple question for Java programmer - I am not sure if it possible directly - please present workarounds.
I want to access parent variable to initialize nested class member but not know the Java syntax to do it (if it possible). How to set Child id with parent id.
public class Parent {
final String id = "parent";
class Child {
// it is invalid since scope hide parent id?
final String id = id;
}
}
The best solution with I found is very ugly see here:
public class Parent {
final String id = "parent";
// ugly clone
String shadow = id;
class Child {
final String id = shadow;
}
}
Please help with syntax - I do not know how to express it.
You can access it by using its fully qualified name:
final String id = Parent.this.id;
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