it will close scanner and shut the warning.


scanner sc = new scanner(;

//do stuff with sc

sc.close();//write at end of code.


the scanner should be closed. it is a good practice to close readers, streams...and this kind of objects to free up resources and aovid memory leaks; and doing so in a finally block to make sure that they are closed up even if an exception occurs while handling those objects.


private static scanner in;

i fixed it by declaring in as a private static scanner class variable. not sure why that fixed it but that is what eclipse recommended i do.


generally, instances of classes that deal with i/o should be closed after you're finished with them. so at the end of your code you could add in.close().


you should close your scanner when you're done with it:



adding private static scanner in; does not really fix the problem, it only clears out the warning. making the scanner static means it remains open forever (or until the class get's unloaded, which nearly is "forever"). the compiler gives you no warning any more, since you told him "keep it open forever". but that is not what you really wanted to, since you should close resources as soon as you don't need them any more.

hth, manfred.


okay, seriously, in many cases at least, this is actually a bug. it shows up in vs code as well, and it's the linter noticing that you've reached the end of the enclosing scope without closing the scanner object, but not recognizing that closing all open file descriptors is part of process termination. there's no resource leak because the resources are all cleaned up at termination, and the process goes away, leaving nowhere for the resource to be held.


if you are using jdk7 or 8, you can use try-catch with resources.this will automatically close the scanner.

try ( scanner scanner = new scanner(; )
    system.out.println("enter the width of the rectangle: ");
    width = scanner.nextdouble();
    system.out.println("enter the height of the rectangle: ");
    height = scanner.nextdouble();
catch(exception ex)
    //exception something (e.g., print the error message)


it is telling you that you need to close the scanner you instantiated on with scanner.close(). normally every reader should be closed.

note that if you close, you won't be able to read from it again. you may also take a look at the console class.

public void readshapedata() {
    console console = system.console();
    double width = double.parsedouble(console.readline("enter the width of the rectangle: "));
    double height = double.parsedouble(console.readline("enter the height of the rectangle: "));


// an inputstream which is typically connected to keyboard input of console programs

scanner in= new scanner(;

above line will invoke constructor of scanner class with argument, and will return a reference to newly constructed object.

it is connected to a input stream that is connected to keyboard, so now at run-time you can take user input to do required operation.

//write piece of code 

to remove the memory leak -

in.close();//write at end of code.


you need call in.close(), in a finally block to ensure it occurs.

from the eclipse documentation, here is why it flags this particular problem (emphasis mine):

classes implementing the interface (since jdk 1.5) and java.lang.autocloseable (since jdk 1.7) are considered to represent external resources, which should be closed using method close(), when they are no longer needed.

the eclipse java compiler is able to analyze whether code using such types adheres to this policy.


the compiler will flag [violations] with "resource leak: 'stream' is never closed".

full explanation here.


as others have said, you need to call 'close' on io classes. i'll add that this is an excellent spot to use the try - finally block with no catch, like this:

public void readshapedata() throws ioexception {
    scanner in = new scanner(;
    try {
        system.out.println("enter the width of the rectangle: ");
        width = in.nextdouble();
        system.out.println("enter the height of the rectangle: ");
        height = in.nextdouble();
    } finally {

this ensures that your scanner is always closed, guaranteeing proper resource cleanup.

equivalently, in java 7 or greater, you can use the "try-with-resources" syntax:

try (scanner in = new scanner( {


because you don't close your scanner


Related Query

More Query from same tag