Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WITH statement in Java

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!

like image 989
Mike Clark Avatar asked Sep 29 '09 20:09

Mike Clark


People also ask

What is with in Java?

with(TemporalField field, long newValue) method of the Year class used to set the specified field of Year to a new value and returns the copy of new time. This method can be used to change any supported field, such as year.

Is there a Java equivalent to with?

try-with-resources is its Java equivalent, and is available in Java 7 and up. This is the try-with-resources construct.

Can you use && in an if statement Java?

When using multiple conditions, we use the logical AND && and logical OR || operators. Note: Logical AND && returns true if both statements are true. Logical OR || returns true if any one of the statements is true.

What does %% mean in Java?

Since % denotes the beginning of format specifier %% is used to escape % char. System.out.println("%%"); prints % For String. indexOf % or %% have no special meaning.


1 Answers

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(); } 
like image 90
Pavel Minaev Avatar answered Oct 11 '22 05:10

Pavel Minaev