2015-12-15 11:47:04 -08:00
|
|
|
// exceptions/AutoCloseableDetails.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-12-15 11:47:04 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-12-15 11:47:04 -08:00
|
|
|
|
|
|
|
class Reporter implements AutoCloseable {
|
|
|
|
String name = getClass().getSimpleName();
|
2017-05-01 14:33:10 -06:00
|
|
|
Reporter() {
|
2015-12-15 11:47:04 -08:00
|
|
|
System.out.println("Creating " + name);
|
|
|
|
}
|
|
|
|
public void close() {
|
|
|
|
System.out.println("Closing " + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class First extends Reporter {}
|
|
|
|
class Second extends Reporter {}
|
|
|
|
|
|
|
|
public class AutoCloseableDetails {
|
|
|
|
public static void main(String[] args) {
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
First f = new First();
|
|
|
|
Second s = new Second()
|
|
|
|
) {
|
2015-12-15 11:47:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
|
|
|
Creating First
|
|
|
|
Creating Second
|
|
|
|
Closing Second
|
|
|
|
Closing First
|
|
|
|
*/
|