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 Getting Started with Java Introduction to Your Tools

Adam Sawicki
Adam Sawicki
15,967 Points

Console console = System.console()

Could somebody explain me this part of code? Especially the part after equals sign.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! What we're doing here is making an object of type Console from the System class that's part of Java and assigning it to a variable named console.

Now that probably doesn't mean a whole lot to you right now, but it will. I promise you will get more in depth on objects and methods later. But here's what it's doing for you right now. You know all those times you have to type console.printf? If we didn't have that, to get the same result we'd have to type System.console.printf every single time.

Here is documentation on the Console class which itself is a member of the System class. https://docs.oracle.com/javase/7/docs/api/java/io/Console.html

Hope this helps! :sparkles:

Sebastian Eguez
Sebastian Eguez
8,248 Points

Hey Jennifer!

What's the difference between "Console" and "console"?

Console console = System.console();
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Sebastian Eguez hi!. The first Console is the class Console which is a member of the System class. The first console is the variable name. In reality, you could name this just about anything, but it makes sense to name it "console" as that's the System class we're sort of making a "shortcut" to.

Hope this helps! :sparkles:

Hi to continue on from what Jennifer stated, even that is too much in my honest opinion if you are just getting started. All you really need to know right now is that the console() method returns the Console instance (Object) to work with it in the form of writing text to the terminal. Do not confuse yourself, you can't know everything and you won't know everything, that is perfectly fine! :-)

Adam Sawicki
Adam Sawicki
15,967 Points

The problem is that I don't understand why I need to use class "System" to create object "console" rather than key word "new" :/