Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Meet Objects Creating Classes

pietro capriata
pietro capriata
18,066 Points

To this example I need to change something

I change the second file:

"public String" -> "public static String"

because without this... appear this msg:

"error: non-static variable car cannot be referenced from a static context "

5 Answers

Juan Arriagada
Juan Arriagada
2,950 Points

Hello There.

Java is an Object Oriented Language, as you might know. When you create a Java Class, you are creating a prototype of an "real life" Object. If you give this object attributes (global variables) and then try to use them directly from an "public static void main(String args[])" you will get that error becose you need to create an instance of your Object Class.

Take this example:

public class Car {
  public String color;
  public String model;

  public static void main(String args[]){
    color = "red";  //<-- HERE YOU WILL GET THAT ERROR
  }
}

But is different if you first create an instance of your class:

public class Car {
  public String color;
  public String model;

  public static void main(String args[]){
    Car chevrolet = new Car();
    chevrolet.color = "red";
  }
}

When you add the "static" element to your variable, you force your class to work as an Static Class. There are several thing you need to have in mind when you're writing Object Oriented software, this is a very, very simplified example.

Hope it helps,

Best regards,

Juan Pablo

Axel McCode
Axel McCode
13,869 Points

"static" is used to indicate that something is a property of the class. Static variables/methods can be used without initializing an object. Non-static methods/variables only exist once you have created an object. So that String must be static in order to be used by other code without creating an object, without the "static" part before your String declaration your code will not know where to look for that string because it will not exist until you create the object.

Hope this helps :)

How can I download Console in UBUNTu and used like treehouse console.

Juan Arriagada
Juan Arriagada
2,950 Points

This is off this topic.

Ubuntu and all Unix based Operative Systems come with a Console (even Windows has one). Look for application called "Terminal". Depending on wich course you're taking, you'll need different subsystems installed in your machine. For example, if you're doing a Java course you will need Java Runtime Environment (JRE) or Java Development Kit. It's the same for NodeJS (you'll need NoneJS in your computer), PHP, etc.

Best regards, Juan Arriagada

I am doing JAVA .. thank you