eclipse result: Resource leak: 'variable' is never closed

import java.util.Scanner; class HelloWorld2 { public static void main(String[ ] args) { Scanner variable = new Scanner(System.in); System.out.println(variable.nextLine()); } } //eclipse result: Resource leak: 'variable' is never closed

16th Mar 2019, 12:34 AM
mokim - avatar
1 Answer
+ 7
add variable.close(); Closing your Scanner means you cut the connection to the keyboard or in other words: your program is not waiting for user input anymore. https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html Most of the IDE's closes the Scanner if you don't use the close method. But I'm not sure what will happen if you have not closed the scanner in an executable Java program.
16th Mar 2019, 1:24 AM
Denise Roßberg
Denise Roßberg - avatar