File: TryWithResourcesVariable.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (23 lines) | stat: -rw-r--r-- 508 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Starting in Java 9, the resource in a try-with-resources statement can be a
reference to a `final` or effectively-`final` variable.

That is, you can write this:

```java
AutoCloseable resource = ...;
try (resource) {
  doSomething(resource);
}
```

instead of this:

```java
AutoCloseable resource = ...;
try (AutoCloseable resource2 = resource) {
  doSomething(resource2);
}
```

NOTE: the resource cannot be an arbitrary expression, for example `try
(returnsTheResources()) { ... }` is still not allowed.