WITH statement in Java
No. The best you can do, when the expression is overly long, is to assign it to a local variable with a short name, and use {...}
to create a scope:
{
TypeOfFoo it = foo; // foo could be any lengthy expression
it.bar();
it.reset(true);
myvar = it.getName();
}
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 have rich enough behaviour. Try refactoring into the called class. Perhaps there is more than one class trying to get out.