As everyone said, this is due to the static type, and the FooBar class FooBar not contain name . So this will not work.
I wanted to point out the proposed use of the Anonymous class.
An anonymous class (or close to Closures, possibly lambdas. Similar, but not the same) comes from the functional programming paradigm, where states must be unchanged.
Saying why you should use such classes? When you need a short and short thing to do that doesn't have to go in full class. Example:
MyTask() //This is a method { new Thread(new Runnable() { //Anonymous class public void run() {} }).start(); }
Understanding the inclusion of your implementation only in a function / class is important.
scope of the variables defined in the Anonymous class (or closed-over function) should only be used inside the Anonymous class , it cannot be accessed from other program code.
Therefore, you should not (and in any case cannot) set fooBar.name = "Test";
zengr
source share