Просто создайте конструкторы для инициализации.
MyClass obj = new MyClass()
{
field1 = "hello",
field2 = "world",
field3 = new MyOtherClass()
{
etc....
}
public MyClass()
{
initialize();
}
public MyClass(String field1)
{
this.field1 = field1;
this();
}
public MyClass(String field, Boolean isField1)
{
if (isField1)
this( field );
else
this.field2 = field;
this();
}
public MyClass(String field1, String field2)
{
this.field2 = field2;
this(field1);
}
public MyClass(MyOtherClass field3)
{
this.field3 = field3;
}
public MyClass(String field1, String field2, MyOtherClass field3)
{
this(field3);
this(field1, field2);
}
private void initialize(){
// Do any class bootstrapping here.
}
}
// Then you have several ways to create an object instance, with initialization
MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass( "Field 1 Value" );
MyClass myClass3 = new MyClass( "Field 1 Value", true );
MyClass myClass4 = new MyClass( "Field 2 Value", false );
MyClass myClass5 = new MyClass( "Field 1 Value", "Field 2 Value" );
MyClass myClass6 = new MyClass( "Field 1 Value", "Field 2 Value", new MyOtherClass() );
MyClass myClass7 = new MyClass( new MyOtherClass() );