Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures 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,407 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