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

My code isn't working

treehouse:~/workspace$ javac Introductions.java
Introductions.java:7: error: non-static method printf(String,Object...) cann ot be referenced from a static context
Console.printf("Abhinav");
^
1 error
treehouse:~/workspace$ ^C
treehouse:~/workspace$

2 Answers

Try "console" with a lower-case "c"...

From the compilation error message, it seems you are trying to invoke a non-static / instance method on a Class (java.io.Console) directly. Static / class methods should / can be invoked on Class and non-static / instance methods should / can be invoked on the instances / objects created from the class. So, first create an instance of "Console" and then invoke methods like "printf(...)" on the created instance as illustrated below:

import java.io.Console;

Console cons = System.console();
if (cons != null) {
    cons.printf(...);
}