Quantcast
Channel: Scratch Where It's Itching
Viewing all articles
Browse latest Browse all 73

AutoCloseable when not available

$
0
0
On his blog, Brian Oxley shows a neat way in JDK 8 to use non AutoCloseable object having a close-like method in a try with resources construct, using the new method references. For those of us who are still stuck with JDK 7 for some time, here is the (uglier) equivalent code:
final Foo foo = new Foo();try (AutoCloseable ignore = new AutoCloseable() {
		@Overridepublicvoid close() {
			foo.close();
		} 
	}) {
		foo.doFoo();
	} 

Viewing all articles
Browse latest Browse all 73

Trending Articles