You can avoid anonymous class
TestClass createTestClass(final String id) {
TestClass testClass = new TestClass();
testClass.id = id;
return testClass;
}
or rename option
TestClass createTestClass(final String theId) {
return new TestClass() {{
this.id = theId;
}};
}
or leave the factory method together by entering the constructor parameter:
class TestClass {
public TestClass(String id) { this.id = id; }
String id;
}
source
share