Search code examples
javaglobal-variablesjava.util.scanner

Is using Scanner as global variable in Java bad practice?


Is it considered bad programming practice to have an input scanner (such as a keyboard) declared as a global var in a class?
Such as:

public static Scanner input  = new Scanner(System.in);

I'm working with alot of input from various methods, and it just seems a lot easier to use a public var then having to send the keyboard to each method.


Solution

  • 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