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

Viewed 4959

I have an example class defined like below:

public class FooBar {

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

}

Later, when I try this:

FooBar fooBar = new FooBar(){
  public String name = null;
  @Override
  void method1(Foo foo){
    ...
  }
};

fooBar.name = "Test";

I get an error saying that the name field does not exist. Why?

8 Answers
Related