Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.