Does Java have a using statement?

Does Java have a using statement that can be used when opening a session in sleep mode?

In C #, it is something like:

using (var session = new Session()) { } 

Thus, the object goes beyond the scope and automatically closes.

+71
java hibernate using-statement
Jan 06 '10 at 21:15
source share
12 answers

Java 7 has implemented Automatic Resource Block Management , which adds this feature to the Java platform. Previous versions of Java had nothing like using .

As an example, you can use any variable that implements java.lang.AutoCloseable as follows:

 try(ClassImplementingAutoCloseable obj = new ClassImplementingAutoCloseable()) { ... } 

The Java interface java.io.Closeable implemented by threads is automatically extended by AutoCloseable , so you can already use threads in a try block in the same way you use them in a C # using block. This is equivalent to C # using .

Starting with version 5.0, Hibernate sessions run AutoCloseable and can be automatically closed in ARM blocks. In previous versions of Hibernate, the session did not implement AutoCloseable . Therefore, to use this function, you need to be in Hibernate> = 5.0.

+88
Jan 6 '10 at 21:17
source share

Before Java 7 , Java had no such function (for Java 7 and above, see Asaph regarding ARM ).

You had to do it manually, and it hurt:

 AwesomeClass hooray = null; try { hooray = new AwesomeClass(); // Great code } finally { if (hooray!=null) { hooray.close(); } } 

And this is just code when neither // Great code nor hooray.close() can throw any exceptions.

If you really want to limit the scope of the variable, then a simple block of code does the job:

 { AwesomeClass hooray = new AwesomeClass(); // Great code } 

But that is probably not what you had in mind.

+25
May 31 '10 at 12:39 a.m.
source share

Since Java 7: http://blogs.oracle.com/darcy/entry/project_coin_updated_arm_spec

The syntax of the code in the question will be as follows:

 try (Session session = new Session()) { // do stuff } 

Note that Session needs to implement AutoClosable or one of its (many) sub-interfaces.

+15
Aug 24 '11 at 8:17
source share

Technically:

 DisposableObject d = null; try { d = new DisposableObject(); } finally { if (d != null) { d.Dispose(); } } 
+8
Jan 06 '10 at 21:18
source share

Nearest java equivalent

 AwesomeClass hooray = new AwesomeClass(); try{ // Great code } finally { hooray.dispose(); // or .close(), etc. } 
+8
May 31 '10 at 12:40
source share

No, Java does not have an equivalent using statement.

+3
Jan 06 '10 at 21:18
source share

Not at the moment.

However, there is an ARM proposal for Java 7.

+3
Jan 06
source share

If you are interested in resource management, the Lombok Project offers @Cleanup . Accepted directly from your site:

You can use @Cleanup to ensure a given resource is automatically cleared before the current code execution path ends. You do this annotation of any local variable using the @Cleanup annotation as follows:

@Cleanup InputStream in = new FileInputStream("some/file");

As a result, at the end of the scope you are in, in.close() . This call is guaranteed to try / finally. Take a look at the example below to see how it works.

If the type of object that you want cleaning does not have a close() method, but some other arguments are a method, you can specify the name of this method looks like this:

@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);

By default, the cleanup method is considered to be close() . A cleanup method that takes an argument cannot be called via @Cleanup .

Vanilla java

 import java.io.*; public class CleanupExample { public static void main(String[] args) throws IOException { InputStream in = new FileInputStream(args[0]); try { OutputStream out = new FileOutputStream(args[1]); try { byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; out.write(b, 0, r); } } finally { out.close(); } } finally { in.close(); } } } 

With Lombok

 import lombok.Cleanup; import java.io.*; public class CleanupExample { public static void main(String[] args) throws IOException { @Cleanup InputStream in = new FileInputStream(args[0]); @Cleanup OutputStream out = new FileOutputStream(args[1]); byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; out.write(b, 0, r); } } } 
+2
May 31 '10 at 13:13
source share

See this Java Keyword List .

  • The using keyword, unfortunately, is not on the list.
  • And also there is no C # using keyword equivalence through any other keyword, as it is now in Java.

To simulate this "using" behavior, you will need to use the try...catch...finally , in which you will manage the resources inside finally .

+1
May 31 '10 at 12:41
source share

ARM blocks from the project coin will be in Java 7. This function is designed to provide similar Java functionality as .Net syntax.

+1
May 31 '10 at 12:42
source share
+1
Aug 13 '14 at
source share

To answer the question about limiting the scope of a variable, instead of talking about automatic closing / controlling variables.

In Java, you can define closed anonymous regions with curly braces. It is very simple.

 { AwesomeClass hooray = new AwesomeClass() // Great code } 

The hooray variable is available only in this area, and not outside of it.

This can be useful if you are repeating variables that are temporary.

For example, each with an index. Just as the variable item closed in a for loop (i.e., available only inside it), the variable index closed in an anonymous area.

 // first loop { Integer index = -1; for (Object item : things) {index += 1; // ... item, index } } // second loop { Integer index = -1; for (Object item : stuff) {index += 1; // ... item, index } } 

I also sometimes use this if you don't have a for loop to provide a scope of variables, but you want to use generic variable names.

 { User user = new User(); user.setId(0); user.setName("Andy Green"); user.setEmail("andygreen@gmail.com"); users.add(user); } { User user = new User(); user.setId(1); user.setName("Rachel Blue"); user.setEmail("rachelblue@gmail.com"); users.add(user); } 
0
Sep 26 '13 at 13:34 on
source share



All Articles