3

How to close scanner class if we did not create object for it to avoid warning messages stating unassigned closable value, resource leak?

As I need to get input only once from user, I did not create reference variable for object Scanner class.

My declaration to get input

int num = new Scanner(System.in).nextInt();
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • 1
    Not possible with your syntax. You must take a reference of it – Ashishkumar Singh Aug 28 '18 at 04:55
  • Theoretically, as soon as there are no active references to the object, it should get destroyed since garbage collection in Java is automatic. – Bugs Buggy Aug 28 '18 at 05:07
  • 1
    Note that if you are making a scanner from system.in, you probably don't want to close it. It actually closes the standard input stream and you won't be able to read anymore input after closing it. – puhlen Aug 28 '18 at 05:16
  • yes i seen it gc will do automatically and also if we did not used any variable or object it will remove and make space available for other object. i just tried to save memory so tried to ignore creating reference variable. thanks for your time bro i assume and got to know scanner class must have reference variable to close. –  Aug 28 '18 at 05:18
  • then why this warning message i am getting @puhlen –  Aug 28 '18 at 05:19
  • 1
    @RaviNarayanan because in general you want to close resources but this is a special, specific case where you might not. – puhlen Aug 28 '18 at 05:21
  • ok bro...thanks much for clearing me out. –  Aug 28 '18 at 05:35

3 Answers3

2

i did not created object for Scanner class

yes, you did: new Scanner(System.in)

In case, you are allowed to use try-with-resource:

try(Scanner sc=new Scanner(System.in)){
  int num = sc.nextInt();
  /* TO DO */
}

OR, just use the .close():

Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
sc.close();
  • Yes i did sorry with new keyword yeah i m aware of that and i used to do how you declared but i am trying to use different way as i am using only once so inside try catch block also my declaration showing same warning message –  Aug 28 '18 at 05:03
  • Only in this way only we need to declare and close when we are using scanner ,so i can't implement in this way in java..right? –  Aug 28 '18 at 05:08
2

Or you can use try/finally:

Scanner sc = null;
try
{
    sc = new Scanner(System.in);
    int num = sc.nextInt();
}
finally
{
    if(sc!=null)
    {
       sc.close();
    }
}
Roman Svitukha
  • 1,302
  • 1
  • 12
  • 22
  • Hi Roman these all i know bro, so this is the only way we need to declare and close..my new declaration is not possible in java right...? –  Aug 28 '18 at 05:06
1

There is no way with your syntax to close the scanner object.

Only one way I can think of right now is, if Scanner would not have been a final class, we could created a class which extends it says ScannerAutoClose and declare/override a finalize method for it and close the object in it using this.close(). The finalize method would have been called at the time of garbage collection so it get's closed automatically. But since Scanner is final class, this is out of the picture.

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41