Merge pull request #7465 from amit2103/BAEL-16045-15

[BAEL-16045] - Check Article Code Matches GitHub for https://www.bael
This commit is contained in:
Loredana Crusoveanu 2019-11-02 17:45:49 +02:00 committed by GitHub
commit 108840fcb4
4 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package com.baeldung.trywithresource;
public class AutoCloseableMain {
public static void main(String[] args) throws Exception {
orderOfClosingResources();
}
private static void orderOfClosingResources() throws Exception {
try (AutoCloseableResourcesFirst af = new AutoCloseableResourcesFirst();
AutoCloseableResourcesSecond as = new AutoCloseableResourcesSecond()) {
af.doSomething();
as.doSomething();
}
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.trywithresource;
public class AutoCloseableResourcesFirst implements AutoCloseable {
public AutoCloseableResourcesFirst() {
System.out.println("Constructor -> AutoCloseableResources_First");
}
public void doSomething() {
System.out.println("Something -> AutoCloseableResources_First");
}
@Override
public void close() throws Exception {
System.out.println("Closed AutoCloseableResources_First");
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.trywithresource;
public class AutoCloseableResourcesSecond implements AutoCloseable {
public AutoCloseableResourcesSecond() {
System.out.println("Constructor -> AutoCloseableResources_Second");
}
public void doSomething() {
System.out.println("Something -> AutoCloseableResources_Second");
}
@Override
public void close() throws Exception {
System.out.println("Closed AutoCloseableResources_Second");
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.trywithresource;
public class MyResource implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println("Closed MyResource");
}
}