Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+19 votes

Are there any specific requirements for where to put Scanner console = new Scanner(System.in) in a static method if we want to read input?

asked in CSC211_Winter2018 by (8 points)

2 Answers

+11 votes

The best practice would be to put that declaration near the top of your main() method, and then pass the console as a parameter into every helper static method that needs access to it.

(However, you can do other things that may also work...)

answered by (13 points)
+9 votes

Concerning the OwlRacing project due a few days ago, the line you're talking about is treated as a class constant. Specifically referring to a line just inside the class header, this is what has been given to us:

public static final Scanner CONSOLE = new Scanner(System.in); // <- don't change this line!

I believe this line is treated as a class constant because its scope is the entire class, and not just one individual method. From this position, any method and any command inside this class can use Scanner type CONSOLE to record user input and establish variables later down the road that are based on said input. I could be wrong, though, but this seems plausible to me.

answered by (8 points)
...