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 Basics Using your New Tools Coding the Prototype

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Console question !

Hi folks,

why do we code

Console console = System.console();
//and not
Console console = new Console();

Thx

Grigorij

4 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hey Grigorij!

System is a class and it has a static public field called out. System.out.

That console method is also static.

Remember the static keyword let's you use the class without creating an instance.

Hope that helps!

Christopher Augg
Christopher Augg
21,223 Points

Lets look at the documentation to answer this question:

According to its documentation, "The System Class contains several useful class fields and methods. It cannot be instantiated."

It provides a console method that "... returns the system console, if any, otherwise it returns null." It is a static method.

The Console Class is not meant to be instantiated either. The Singleton Design Pattern is followed for its implementation to ensure that only one instance of the Console class is ever created. It states within the documentation, "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."

I hope this helps.

Regards,

Chris

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

So you take the System class and use the dot (.) operator to access the methods inside the System class. But what is this magic Systen class used for???

Why do we call:

Syste.out.println ...

Does the System class has the out() method????

Grigorij

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Craig,

thanks !!!!

Now I understand :)