About Question enthuware.ocpjp.v7.2.1210 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

About Question enthuware.ocpjp.v7.2.1210 :

Post by horst1a »

Hello ,
I 've always thought, when using a try-with-resource statement the class has to implement AutoCloseable
but the following code compile without:

package food;
import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;

public class IOTest {
public static void main(String[] args)
{ try(BufferedReader bfr = new BufferedReader(new FileReader("c:\\works\\a.java"))){
String line = null;
while( (line = bfr.readLine()) != null){
System.out.println(line); }
}catch(IOException e){
e.printStackTrace(); } } }

What happens to opened resources if class doesnt implement AutoCloseeable? Will they stay opened ?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1210 :

Post by admin »

horst1a wrote:Hello ,
What happens to opened resources if class doesnt implement AutoCloseeable? Will they stay opened ?
Yes, exactly, you would have to close them explicitly in finally block. That is why try-with-resources/AutoCloseable was introduced in Java 7.

Should go through this tutorial : https://docs.oracle.com/javase/tutorial ... Close.html

HTH,
Paul.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1210 :

Post by horst1a »

Thanx, Paul,

thats what i thought would happen

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests