Hello my fellow programmers.
I have some code which the spring tool suite editor responds differently too, maybe some of you smart people know why.
File inputFile = new File(System.getProperty("user.home") + "/Desktop/Input.txt");
Scanner sc = null;
double payment=0;
try {
sc = new Scanner(inputFile).useLocale(Locale.ENGLISH);
//sc = new Scanner(inputFile)
//sc.useLocale(Locale.ENGLISH);
payment = sc.nextDouble();
} catch (Exception e) {
} finally {
if (sc != null) {
sc.close();
}
}
Can someone tell me why this is not possible:
Scanner sc = new Scanner(inputFile).useLocale(Locale.ENGLISH);
but this is!?
Scanner sc = new Scanner(inputFile);
sc.useLocale(Locale.ENGLISH)
the first line up code gives me a warning "Resource leak: unassigned closeable value is never closed", and as you can see i've in my try/catch used a finally which should always close my object if it's not null.
Thank you in advance.
It's the compiler warning you about something it doesn't (or rather can't) understand.
When
useLocale()
is called, the sameScanner
instance it was called on is returned. However the compiler can't know that and issues a warning. If the method returned a differentScanner
object, the resource leak would be valid as the originalScanner
object would have disappeared without being closed.An unwarranted warning, that you may ignore.