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

How to actually Declare a Class "?"

For some reason I followed all of the steps on the video and my class for some reason isn't being declared in the way that the video explained. Also, even when I get past that part, there is no explanation in the videos about creating fields and storing variables within fields. Could you make this more clear to me so I can actually know how to do such a thing?

2 Answers

Hi there! I'll give an example of declaring a class called "Car".

public class Car {

}

This by itself doesn't actually do anything. If this Car class is going to be the only class\file in our program than it needs to contain the main method. This is the first thing that executes when the program starts.

public class Car {
   public static void main(String[] args) {
   //code to execute, such as System.out.println("hello world");
   }
}

With the above code, we would have a file named Car.java which is also our class name. If we still have the Car.java as our main file, and wanted to create a second class called "Fuel", it would would be similar to the first example, where you do NOT want a main method, as there should only be 1 (the one in the car class).

public class Car { 
   public static void main(String[] args) {
   //code to execute, such as System.out.println("hello world");
   }
}

public class Fuel {
//variables, methods, possibly constructor
}

I hope this helps give you a larger perspective on the structure of the program. If you have not seen them yet, there are videos that will go over member variables for classes, and class constructors that you use to set initial values for variables in a class.

If you have seen the videos and are still confused or would like me to explain constructors and class variables let me know in a comment here.

Take care and happy coding :)

I listened to the whole video and the video doesn't explain or even talk about a field with a class variable of the type String to store a variable into the class variable. It also doesn't explain how to set a value to a certain variable/constructor. That is why I'm stuck on the second part.

I am not sure what video\challenge you are on but I will do my best to explain constructors and setting field variables. Below is commented code:

//say we have a class named Dog

public class Dog {
   String name;
   int age;
//These are example of declaring member variables
//You can declare as many as you want or any type, int, String, objects, etc.
}

These two member variable we have declared above. We do not want to give these values yet. Say we declared String name = "Spot"; Then every Dog object is named "Spot" We don't want this. Now to add constructors:

public class Dog {
   String name;
   int age;
      public Dog(String name, int age) { //This is our constructor. We give it parameters of the values we want to set
          this.name = name;                        //in this example it is the Dogs age and name member variables
          this.age = age;
          //"this." keyword refers to the variable declared directly under the "public class"
         //The other variables without this refer to the local variables inside the constructor
   }
}

Now we can use this by declaring an object, and we set the member variables in the constructor, example:

public static void main(String[] args) {
Dog dog = new Dog("Spot", 7);
//Now we have a dog object with name="Spot" and age=7
}

Hope this helps! Take care and happy coding!

Definitely a good answer from michaelcodes on how to lay out the classes. Can you expand on your question a bit more?

For some reason I followed all of the steps on the video and my class for some reason isn't being declared in the way >that the video explained.

What do you mean when you say it isn't being declared in the way you are expecting? Can you post your code for us so we can review it with you? Or is there an error message you are receiving?

Thanks in advance.

Zac