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 Perfecting the Prototype Reviewing Our Feedback

Canbin Mei
Canbin Mei
428 Points

why do we need this line? Console console = System.console();

Why do we need this line in our program?

Nino Roldan
Nino Roldan
Courses Plus Student 9,308 Points

To instantiate the System.console() static class. This class is (among other things) is used to read from the console.

2 Answers

Gavin Ralston
Gavin Ralston
28,770 Points
Console console = System.console();

So on the right you're using the System class to create a console object (through it's console() method) and then assigning it to a variable designed to hold a console (the capital C Console) and storing it in a variable named (lower case) console :)

Not trying to be sarcastic about it. I don't know what your level of understanding is just yet.

The Console object has a few really useful methods, and is how you're going to be able to read input from the user. Mainly you'll be using the .readLine() method with a prompt string passed into it to prompt users for input.

Canbin Mei
Canbin Mei
428 Points

I don't understand what object and class are, Console console on the left hand side of the equal sign really confuse me

Per Karlsson
Per Karlsson
12,683 Points

Canbin Mei , the capital C Console is the object and the lower case console is just a variable.

You can call it whatever (with some restrictions of course) you want. Like this for example.

Console c = System.console();

//or

Console myFancyConsole = System.console();

I think that the confusion for new Java users lies in using console three times. I know they're referring to completely different things, so why don't I take a stab at metaphoring this for Canbin (and me).... Ok so Canbin the phrase 'Console console = System.console()' could be changed to this:

'Sub BMT = Subway.sub();'

So let's say you're ordering a sub at subway. The worker uses a touch pad that enters your order as "Sub, Sub Sandwich, BMT" and then gets to work, but what's happening behind the scenes of the worker's touch pad is: the Subway computer library (like a database) opens the library item called sub (where they keep all the names of their subs)...You ask for a BMT. So the library creates a specific instance (or your specific sub order) from the Subway.sub library...and the type of your sub is a BMT.

So you could read the code 'Sub BMT = Subway.sub();' as Your BMT sub is a individual example of a Subway sub.

So Subway is a library item (which is a Java term kinda like a category library at a library, like mystery books) sub is a constructor (it makes an object of a given type) Sub (uppercase) is a specific example/instance of the sub constructor (so it's saying I'm making a special sub just for you!) BMT is a specific object created from the Sub instance (your special sub order)

I'm still learning this too Canbin, so I hope I'm right and not just confusing you further!

Gavin Ralston
Gavin Ralston
28,770 Points

Maybe to put it another way, I'll try an english representation of this line:

Console console = System.console();

It's sort of saying the following:

"I'm going to store a Console object in the variable I called 'console'. Hey System, give me a Console to work with."

So breaking down that sentence:

"I'm going to store a Console object and name it 'console' so I can access it later..."

Console console;  // this says what type of variable it is, then names the variable

The first "Console" is telling Java what type of variable it is, the second "console" is the name you gave it so you can work with it later.

The third console() is a method which the System uses to create a console object for you to store.

"Hey System, give me a Console to work with..."

System.console();  // this gets a console that works with your computer

Put them together and the Console that is returned from that method call gets assigned to the variable you just created: A variable that holds Consoles, which you named console, which you got by creating a console object through a System method. :)