0

This is a fairly simple question, but I'm curious if it is standard to always close things like Scanner before the application exits.

For example, if I always want Scanner searching for next input in my application- if the application is closed should I close the Scanner object before exiting the application?

Scipher
  • 3
  • 2
  • 3
    Typically, when a process terminates, all I/O resources that remains to be still open are handled by the operating system. In the case of Java, it could be the JVM instead of the OS. – mostruash Sep 09 '14 at 18:13

1 Answers1

0

You should try to clean up resources as you go. When you are done with them, close them (except System.in unless you know it won't be used again)

In the case of a shutdown, this can be ignored, but in general you shouldn't assume a program is about to shutdown, and using try-with-resources is a better option.

try (Scanner in = .....) {
   // use "in"
}
// you might add code here later.

In short, you don't need to worry about it in your case, but it would be best practice to clean up as you go.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thank you! I assumed this was the case, so even though it isn't really necessary in my case I'm going to do it anyways; I've found out the hard way on a few occasions how difficult bad habits can be to break! – Scipher Sep 09 '14 at 18:35