WITH statement in Java

Mike Clark picture Mike Clark · Sep 29, 2009 · Viewed 23.5k times · Source

In VB.NET there is the WITH command that lets you omit an object name and only access the methods and properties needed. For example:

With foo
   .bar()
   .reset(true)
   myVar = .getName()
End With

Is there any such syntax within Java?

Thanks!

Answer

Pavel Minaev picture Pavel Minaev · Sep 29, 2009

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();
}