Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Creating the MVP Prompting for Guesses

Brendan Passey
Brendan Passey
999 Points

Prompter prompter = new Prompter(game); Scanner scanner = new Scanner(system.in); Wha?

I barely know what question to ask about this to be honest, my understanding is clearly limited. I know this line has something to do with avoiding naming collisions..... and probably creating an instance of the class prompter.. Why are we doing this? I've been fine so far but this video seems to contain a bunch of stuff I just don't understand.

Why do we need to create a new instance of scanner? Also why won't just "Scanner = new scanner(system.in);" suffice? Like what does the second "scanner" do? Also why is it lower case as opposed to the other two "Scanner"s? I feel pretty lost.

2 Answers

Steven Parker
Steven Parker
231,236 Points

Creating a scanner instance in "promptForGuess" makes its "nextLine" method available to use for getting input.

And in the declaration, "Scanner" (capital "S") is the type of thing being declared, and "scanner" (little "s") is the name of the variable that will hold the reference to the object. The different case makes it a completely different identifier to the computer, but keeping the same name makes it easy for anyone reading the code to know what kind of thing it is.

Brendan Passey
Brendan Passey
999 Points

Thanks guys I took a break and came back and worked through it all again. I understand this stuff now but thanks for the answers!