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 (Retired) Creating the MVP Prompting for Guesses

naga bonar
naga bonar
3,338 Points

why does the console object here did not use "new"?

Hi there,

May somebody help me explain why does the console object in the promptForGuess method did not use "new"? Why does it use static method directly?

I believe it is because "new" would create a new instance of a class, where as with System, from the JavaDocs, there is only one console for a particular virtual machine. I had the same question back when watching Java Basics when we were printing to the console.

https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#console() console public static Console console() Returns the unique Console object associated with the current Java virtual machine, if any. Returns: The system console, if any, otherwise null. Since: 1.6

1 Answer

We don't use new because it already exists. Think of System.console() as a program/app on Windows or Mac. Console console= is essentially creating a shortcut (Windows) or alias (Mac) for the program/app (in this case, the program/app is our console that we write commands in and receive feedback in).

Essentially it's a nickname to refer to the object, not the object itself. It's a bit easier to read and type if you call it "console" instead of "System.console" all the time. And we DON'T want to create a new program/app because we can only see one console at a time--the one that's already created.

naga bonar
naga bonar
3,338 Points

Thank you Adam and Shaun.. I can see it clearer already.