Reputation: 7519
I have a class diagram like this:
There is a parent abstract class called BaseParent
with an inner static class InnerStatic
. Two classes are extending it and it's inner class - Foo
and Bar
(this is always the case).
What I want is to have a field set for the base object
BaseParent parent = new Foo();
parent.setField(Field a);
Then inside the definition of FooInner
and BarInner
, I want to access that field that I previously set by calling setField();
public abstract class BaseParent{
public void setField();
public static class InnerStatic {
}
}
public class Foo extends BaseParent{
public static class FooInner extends InnerStatic{
public void dummyMethod(){
// access field here
}
}
}
public class Bar extends BaseParent{
public static class BarInner extends InnerStatic{
}
}
I tried to make the field
and setField()
static but then as soon as Foo called setField
Bar's field
would be changed too because it was static and the same.
How could I achieve this?
Upvotes: 1
Views: 119
Reputation: 802
If I get what you want to do correctly I think this should work inside FooInner:
Foo.this.getField();
This will give you a hold of the outer class object from the inner class object. Then When you get hold of class Foo you can now call it's super implementation.
Upvotes: 1
Reputation: 2355
Brutally said, not. Field
is owned by class BaseParent
, and therefore Foo
and Bar
are both using the same Field
.
A workaround you could consider, if you REALLY want to do it this way, is using a static HashMap<Class,InnerStatic>
. The "normal" way to go would be to just let Foo
and Bar
have their own static InnerStatic.
Upvotes: 0