Why does adding a public field to an anonymous class in Java not work?

后端 未结 8 1593
渐次进展
渐次进展 2020-12-17 15:48

I have an example class defined like below:

public class FooBar {

  void method1(Foo foo){ // Should be overwritten
    ...
  }

}

Later,

8条回答
  •  伪装坚强ぢ
    2020-12-17 16:33

    You're creating an object of type foobar. The compiler only knows about the members defined for the class/interface foobar.

    Remember, java is a static language, not dynamic. It doesn't check the object at runtime for what exists, it checks at compile time based on the type declaration.

提交回复
热议问题