what does this mean? and how do I fix this issue?
resource leak:'input1' is never closed
many thanks.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
what does this mean? and how do I fix this issue?
resource leak:'input1' is never closed
many thanks.
If a class implements the interface Closeable and you create a new instance of this class then you are also supposed to call the "close()" method on the object at some point in time.
If you dont the compiler might generate a warning just like the one you have posted.
Thank you Cornix.
I actually just ended up converted local variable to field. It fixed the problem, but I have no idea why this fixes the problem. Do you mind explaining please. thank you
Depending on if you are using basic console I/O with the Scanner class,
you can ignore the warning. Like Cornix suggested, it regards mainly file
streams, that a file object must be closed when opened. When you use
class Scanner, you create an instance of the object, and this opens
the Scanner port - so the compiler is reminding you to close it.
Wishes Ada xx
If to Err is human - then programmers are most human of us all.
"The Analytical Engine offers a new, a vast, and a powerful language . . .
for the purposes of mankind."
— Augusta Ada Byron, Lady Lovelace (1851)
wamidh (September 11th, 2014)
Because the compiler is not smart enough to detect that you are doing something wrong and therefor does not give out a warning. But the actual problem is not solved at all, it has just been hidden from the view of the compiler.
The actual solution would be to call the close() method on your object once you dont need it anymore, as I have explained in my first post.
oh. how do I use close()
do I type in close(input1) ;
thank you Cornix
--- Update ---
ah, thank you Ada
If you have a variable of type Closeable then it has a close() method.
For example:
Closeable someCloseable = new Scanner(System.in); someCloseable.close();
Because the Scanner class is implementing the interface Closeable. Other Closeables could be all kinds of Output- or InputStream's
wamidh (September 11th, 2014)
If you are using Java 7 or above, you can use the try-with-resource:
try (Closeable someCloseable = new Scanner(System.in)) { ... }
If I am to understand the Oracle documentation correctly, that should automatically close the resource when the try block is exited.
NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:
When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.
Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/
wamidh (September 11th, 2014)
wamidh (September 11th, 2014)
Only if the class is implementing the interface AutoCloseable which the Scanner class actually does, so it should work.
But not every Closeable is neccessarily an AutoCloseable as well.
wamidh (September 11th, 2014)
thank you everybody!