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

What is class in Java?

In "Creating Classes" course Java.

He used

line 1: class PezDispenser {

line 2: }

what does class mean??? can you make it easy to understand please?

4 Answers

The answer to this question is the very concept of object-oriented programming.

I will try to make it easy as possible to understand for you, but only on the basic level youre gonna need it for. Obviously the whole concept is a little more complex but you don't need to know those things at the moment (interfaces, enums, inheritance, wiring up a UI etc.)

I'm gonna tell you a story.

There is a mystical land called programotopia(program), which is inhabited by robots called objects. Those robots are made in the so called classes(kind of like factories where objects are made). The objecs are the administrators of programotopia, every action there is performed by them. There is an object that cleans the streets, there is one that cooks the meals for everyone and theres even objects that can do more than just one task. Take a look at mister 'scanneyTheScanner' for example, he was produced in the Scanner-factory (java.util.Scanner class). He can do lots of things for you, but you have to tell him what you want him to do otherwise how would he know what to do? If you tell scanneyTheScanner to go to the Console and wait for the next line of input like this:

"String input = scanneyTheScanner.nextLine();"

he's gonna bring back a line of text that someone has put into the console in the form of a String and put that String into a String box(String variable) and write 'input' on that box (lets say the input was "blueberries"). You now have a String box(String variable) called 'input' and inside is the String value "blueberries".

In order for scanneyTheScanner to exist he has to be produced in one of the object-factories first though. So you can say:

Scanner scanneyTheScanner = new Scanner(System.in);

What this does does is it tells the object-factory(class) called 'Scanner' to produce a new Scanner called scanneyTheScanner who is a Scanner that takes input from System.in (this is the console usually, it is in eclipse and intellij idea for example, but this isnt so important to know at the moment).

This order to create a new object that you're giving the object-factory is called a constructor(makes sense right?).

Ok so to summarize:

  • There is a thing called program
  • There are things called objects that can do things and those things the objects do is what makes a program do something
  • The things objects do are called methods by the way
  • CLASSES are the factories where you can produce an object, which has certain properties(fields/member variables) and things which that object can do(methods) depending on the factory you're using. If you want a new boat you go to a boat factory not to a car factory.

I really hope this doesnt confuse you more than it helps you, i know it helped me to make this sort of model of the object-oriented-programming-world when i had to deal with the basics of it.

best of luck and fun with your learning

Matthias :)

I like your analogy

Hi,

I assume you know what a string is. A string has methods that belong to it like the .length() method that returns the length of the string. You can think of a class as a type. And you can create your own classes therefore you can create your own types . Let's say you write some application and you want to create a 'Car' type. To do so, you first need to create a Car class. Something like i did below. Usually a class is put in its own file. It acts as a blue print so you can create as many cars as you want in your application. You can even give access to this 'blueprint' to other developers so they can also create cars without having to rewrite their own blueprint for a Car. That would be re-inventing the wheel. You know what I mean? Anything can be a class. Usually anything that you can think of as a noun can be a class. Than class can have methods which are verbs like 'drive'. That class can have properties which are like adjectives like the color of the car or size.

public class Car {
      //all the cars properties go here -> the color of the car, build, how many wheels, what the max speed is and the amount of gas available
      private string color;
      private string build;
      private int wheels
      private double maxMph;
      private int amountOfGas = 10

     //we have to create a constructor so we can 'instantiate' an object of the type 'Car'
    // notice that i left amountOfGas at 10 as every car that I instantiate starts with a full tank by default
     public Car(string pColor, string pBuild, in pWheels, double pMaxMph) {
         //we assign the parameters to the properties of our car
         pColor = color;
         pBuild = build;
         pWheels = wheels;
         pMaxMph = maxMph;

     }


     //We can also create public methods so you can call them. similar to string.length() to find the string length
     //We can create a method that 
     public int drive(int distance) {
           //when the car drives we need to subtract the amount of gas used
           //this is a over simplified version - as you can add logic so that an error is thrown when the distance is more than         the available gas
           amountOfGas = amountOfGas - distance
           //we then return the amountOfGas 
           return amountOfGas;
      }
}

//So then we can instantiate an object of a car like this -> by using the constructor we created above
Car myCar = new Car("red", "Nissan", "4", 130.5);

//I can then tell my car to drive and find out immediately how much gas I have left after it
double gasLevel = myCar.drive(5)

//gasLevel would be at 5 now

Classes aren't easy to learn as a beginner, and it was even really challenging for myself to learn when I was first learning programming

Basically, classes are a blueprint for something.

Think of it as this. When you make cookies, you often use cookie cutters. The cookie cutters are creating instances of itself. When you want to eat, you don't eat the cookie cutter class itself, you use (eat) its instances.

However, like I said, in order to make those instances, you need the class.

The instances don't have to be the same, for example, you can add some food coloring, and then you would have green cookies.

They all have the same shape, though.

Here's another example. Think of a car. You have a model of it, and you create an instance of it. The person using the car doesn't drive the model, they drive the instances of the model.

Also, classes can have instance methods, which are actions that classes allow instances can do. For example, our Car class might have a drive method, allowing the car to move. Cars also have attributes, or data, like it's color, maximum speed, size, amount of wheels, brand, et cetera.

Craig will explain classes in detail in a couple of videos :wink:

I hope this helps :grin:

Happy C0D1NG! :tada:

:dizzy: ~Alex :dizzy:

Hi,

thank you all for your very helpful response