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 Review: Introduction to Your tools

Why use a Console object at all?

To use console.printf() we first have to import java.io.Console and declare console as a Console and set it equal to System.console... How is the Console class helping? It's doing something, because if the Console declaration is changed to an Object declaration, the printf() method cannot be called.

Also, why can we call System.out.printf(); without importing anything? Also, why can we call consule.printf(); where consule = System.consule; but we cannot call System.console.printf(); ?

Okay... So maybe it's illegal to call System.console.printf() because the Console class does not declare the printf function as static whereas the System.out class does.

1 Answer

Brandon Zeck
Brandon Zeck
3,127 Points

Michael McDonald,<p>

Please check the links below:</p><p>

Console</p><p>

Console, Tracing and Logging</p><p>

In simple terms, Console is a window or a part of a window where you may input command-line arguments and/or see your program output.</p><p>

Calling printf() using some random Object obj will throw error because the particular Class, whose Object is obj, may not have a printf() method/function implemented within it.</p><p>

We can call System.out.printf() as well as System.console().printf() but not System.console.printf() as can be seen in the code below.</p>

import java.io.Console;
public class Introductions {
    public static void main(String[] args) {
        System.console().printf("Hi\n");
        System.out.println("Hello");
  }
}

<p> This is because there is no field by the name of console in the System Class.</p><p>

The afore mentioned style of calling members of a class are examples of using Fully qualified name.</p><p>

Please click to see Wikipedia article on Java syntax of using fully qualified name here. </p><p>

Hope this helps.</p><p> Please Up vote if it helps. </p><p> If you feel this is the answer that you were looking for, please `Mark as Answer'</p><p> And in case, it does not, please reply so that we will get to know.</p><p>

Thanks.</p>