Perhaps the closest way of doing that in Java is the double brace idiom, during construction.
Foo foo = new Foo() {{
bar();
reset(true);
myVar = getName(); // Note though outer local variables must be final.
}};
Alternatively, methods that return this
can be chained:
myName =
foo
.bar()
.reset(true)
.getName();
where bar
and reset
methods return this
.
However, wanting to do this tends to indicate that the object does not ahve rich enough behaviour. Try refactoring into the called class. Perhaps there is more than one class trying to get out.