0

A little help :) (Java)

https://code.sololearn.com/cA1A23a6A9A3/#java can you look into my code and tell me why we use "private static final Scanner scanner = new Scanner(System.in);" I know the uses of final but what consequences will I face if I don't add "final" while initializing Scanner object?

1st Jan 2021, 5:23 PM
Ray
Ray - avatar
3 Réponses
+ 5
Rabeeh Final means you cannot reassign anything. Final can be use in 3 cases: 1. When you don't want to change the value 2. When you don't want to override method 3. When you don't want to inherit the class Here scanner is a variable of Scanner class. So doens't matter of final. You can make a variable final but you cannot reassign value. Just write scanner = new Scanner (System.in) you will get an error. "cannot assign value to final variable scanner"
2nd Jan 2021, 8:34 AM
A͢J
A͢J - avatar
+ 3
Rabeeh You have to import util package because Scanner class exist in this package. import java.util.*;
1st Jan 2021, 6:20 PM
A͢J
A͢J - avatar
0
Beside that why we use final here? Look if we remove the "final" the code will work as normal. I want to know is that a convention among coders to use final in such cases? I Am AJ !
2nd Jan 2021, 6:51 AM
Ray
Ray - avatar