This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes close() for I/O objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "close() for I/O objects" Watch "close() for I/O objects" New topic

close() for I/O objects

Shawn Kuenzler
Ranch Hand

Joined: Apr 16, 2006
Posts: 73
Let's say you've got an exam question on I/O that shows you've got to invoke the close() method and have to choose which reference to invoke it on. Either closing the fileWriter or the bufferedWriter will compile and execute without problems. So which do you choose?

SCJP 1.5
Deepak Bala

Joined: Feb 24, 2006
Posts: 6661

You need to add the word boolean before newFile for compilation to succeed. You can close either the fr or the br. It doesnt matter. Not closing them is still legal but inefficient.

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Shawn Kuenzler
Ranch Hand

Joined: Apr 16, 2006
Posts: 73
Yeah, I didn't copy in all of the code from the example in the book.

Regardless, there was a drag and drop question on the exam with this issue. So I guess my question is, will the exam accept either fr.close() or br.close() as a correct answer? I'm not sure if the Prometric exam has the logic to allow for multiple correct answers in one item.
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
I think you should use br.close()
because from java api
I know if you invoke br.close()
fr will close bacause this method is inside br.close()
Herb Tybur

Joined: May 12, 2006
Posts: 19
Can anybody please give a closure on this? The API for BufferedReader doesn't say anything like that.
I agree. Here's the link:
subject: close() for I/O objects