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

Object Reference Variables

I always have a problem understanding this aspect of object reference variables. Can someone please assist.

Console console = System.console();

in a normal object reference it would create a new object "Console" and assign that to the name of the reference variable (console). But this one i'm not sure about. Console is being accessed/initiated and it is referencing System.console? is System.console apart of console? please assist.

2 Answers

Saiteja Vemula
Saiteja Vemula
7,162 Points

Console console = System.console();

In this line, the first word, "Console" is a class. The second word "console" is an object of type "Console" class. This "console" object is used to take input from the console (i.e., the terminal where you compile and/or run your java program). You are doing this by assigning "System.console()" method which reads the input from console.

Okay follow-up question, so you now have an object of type Console and your passing in the System.console() method. can you pass any method into the object of type Console?

Saiteja Vemula
Saiteja Vemula
7,162 Points

I'm not much familiar with Console class. But, to the knowledge I possess, as you've created an object named "console" of type Console class, you can use methods present in Console class and use them on "console" object.