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

后端 未结 8 1609
渐次进展
渐次进展 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:23

    A local class would do

    {
        class MyFooBar extends FooBar{
            String name = null;
            ...
        };
    
        MyFooBar fooBar = new MyFooBar();
    
        fooBar.name = "Test";
    }
    

提交回复
热议问题