Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Serialization

Andrew D.
Andrew D.
2,937 Points

Not Necessary to Close Object Output Stream?

Hey guys.. I noticed Craig didn't end up using the oos.close() code. I come from C++ where closing an output stream is vital. Is there some new quirk in Java that doesn't make it necessary to close an object output stream?

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

Thanks to a relatively new feature introduced in Java 7 they'll be automatically closed after the try/catch block. The I/O classes have an interface called AutoCloseable. Anything opened in the parentheses after try with that interface are taken care of.

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html