Junilu Lacar wrote:I concur, it's a bad idea. In fact, I'll go one step further and say it's a horrific idea. Remember, an interface is primarily a definition of behavior. Don't make it a bucket for some infrastructure utility just because it might make some things easier to do. Coupling an interface to an implementation like that is bad, bad, bad. Don't do it, no matter how useful you might think it would be.
If you do a search, there are a few moderators who have their own utility classes for console keyboard input. This is probably the one time I wouldn't cringe at the thought of a singleton. Putting a static instance in your main class is still a bit of a misplaced responsibility. However, if the program is small enough and won't really be maintained long term, then it's not so bad. You have to strike a balance between simplicity and strict adherence to design principles.
Kevin O'Sullivan wrote:Would a better option be just making the scanner instance static in your main class? and then you could use it across multiple classes.
There is a worse problem than new people not understanding Scanner. I think lots of people, even experienced people, don't understand it, and (as Winston Gutkowski pointed out ages ago) I haven't seen many good tutorials about it, and books are often not clear about its vagaries. People find out it has vagaries, trip over them, and then dismiss the class as useless.
Matthew Bendford wrote:. . . new devs new to Java not correctly understanding how to use Scanner . . . .
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton