Scanner object | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Scanner object

Why should I close the scanner after leaving? Am I supposed to do this using all instances even in case of just receiving input from user? Eclipse warns me about a resource leak. --- And also the same kind of question. Is it good practice to use static methods? I recall hearing that it is not sometimes.

30th Apr 2017, 3:53 PM
Alex Snaidars
Alex Snaidars - avatar
2 Answers
+ 6
Yeah!! Java Give a pain to my ass 👍😝
30th Apr 2017, 4:15 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 1
I’ll be simple. There is no need to close the scanner. Not closing might generate some warnings although. As soon as the block in which you defined Scanner object is over the garbage collection does your job for you. But until it is done the resources allocated for Scanner are still there and will be there until block(or process) is over. To optimize your code( to make it faster and efficient) it is better to close() objects as soon as their work is done. No it is not a good to make many function static, not at all if you are working with big project it will become pain in the ass.
30th Apr 2017, 4:08 PM
Aditya kumar pandey
Aditya kumar pandey - avatar