I am trying to construct a subclass like this
abstract class Foo
{
abstract BiConsumer getConsumer();
Foo(Object value)
{
getConsumer().accept(this, value);
}
}
class Bar extends Foo
{
Object value = SOME_OBJECT_1;
@Override
BiConsumer getConsumer()
{
return (object, value) -> ((Bar)object).setValue(value);
}
Bar()
{
super(SOME_OBJECT_2);
}
void setValue(Object value)
{
this.value = value;
}
}
but using a HashMap to store different things to be initialized in different subclasses.
The problem is, when I do this, and run
System.out.println(((Bar)this).value);
after accepting the consumer in Foo
's constructor, I get, as expected, SOME_OBJECT_2
. However, when I run
System.out.println(this.value);
after the super
call in Bar
's constructor, I get SOME_VALUE_1
.
Why is this and how can I solve it?
Here is my superclass for reference, and here and here are some examples of subclasses