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!

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 Current Progress

Alan Mattingly
Alan Mattingly
9,298 Points

Creating an instance of the Console class

We have learned that, to create a new instance of the PezDispenser class, we write: pezDispenser dispenser = new PezDispenser();

And, to create a new instance of the Game class, we write: Game game = new Game("treehouse");

But it seems that, to create a new instance of the Console class, we write: Console console = System.console();

To create a new instance of the Console class, why don't we write: Console console = new Console(); ??

1 Answer

Geovanie Alvarez
Geovanie Alvarez
21,500 Points

Take from docs.oracle.com

Methods to access the character-based console device, if any, associated with the current Java virtual machine. Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.