Search code examples
javainner-classes

read parent class members from inner class get old values


I have simple an inner class declared as such private class ColumnResolver that is instantiated in the parent class like that private final ColumnResolver columnResolver = new ColumnResolver();

When I when access members from the outer class from the inner class (OuterClass.this.InnerClass), members have wrong values.

intellij screenshot

I expected to have 3 and 8 and not 0 and 0 as seen in the picture.

There is no multi-threading involved, I don't understand what's going on...

Also, it seems like this and this$0 don't reference the same object? (not the same id).

It's like the VM assigns a cloned object to this$0 and not the actual parent class instance.

What could possibly cause this?

$ java -version
openjdk version "11.0.14" 2022-01-18 LTS
OpenJDK Runtime Environment SapMachine (build 11.0.14+9-LTS-sapmachine)         
OpenJDK 64-Bit Server VM SapMachine (build 11.0.14+9-LTS-sapmachine, mixed mode)

Solution

  • I figured out Mockito was messing up things.

    That is why I had different instances of the inner and outer class.

    When I wrap the outer class with spy(), it messes things up, and the inner class reads/writes from some cloned object...