0

I mean in a program that takes input more than once, would I need to close the object after each use, or just after the last input? sorry noob here

thanks for reading :)

Ninorin
  • 35
  • 5
  • If you are using it for input from *keyboard* the best approach will be to **never** close it. – PM 77-1 Mar 19 '21 at 19:34
  • in what case would you recommand closing it? / when do I need to worry about a resource leak – Ninorin Mar 19 '21 at 19:39
  • You really should close a scanner object once you are done using it. This is good practice and guarantees no memory leak. Memory leaks are more of an issue in programs that run for a long time or require a lot of dynamic memory. Just think about an operating system, a memory leak would be detrimental. – Shaan K Mar 19 '21 at 19:51
  • When you work with actual files - close at the end. Quite honestly - once you progressed beyond basics, you wouldn't be using `Scanner` anyway. – PM 77-1 Mar 19 '21 at 19:52
  • are there any cons to (closing a scanner object) ? – Ninorin Mar 19 '21 at 19:57

1 Answers1

1

You should close your scanner when you are done using it. This prevents memory leaks.

But you definitely don´t have to close it after every keyboard input.

Of course closing the object takes computing time but I don´t think you have to worry about this as a starter.