Is it considered bad programming practice to have an input scanner (such as a keyboard ) declared as a global var for a class? such like:
private static Scanner input = new Scanner(System.in);
Im working with alot of input from various methods, and just seems alot easier then having to send the keyboard to each method
It seems a lot easier to use a global variable, but in the long run, it can make the code very hard to maintain, have you thought about creating a class to handle the keyboard input? By having a good separation of concerns, you end up with cleaner code.
https://en.wikipedia.org/wiki/Separation_of_concerns
It's best if you created a special class for getting Inputs and/or produce Outputs for example
class IO{
Scanner scanner ;
public Scanner getScanner(){
return new Scanner();
}
public Scanner getScanner(File file){
return new Scanner(new FileReader(file));
}
... // other types of scanners
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With