Java is an object-oriented programming language. What this means is that basically everything in Java can be thought of as an object. An object is a collection of properties and methods that describe a single entity. A class is a blueprint for creating those objects.
Creating a Class
Creating a class is simple. Simply use the class keyword and then give it a name.
Let's create a Pet class inside Pet.java:
JAVA
publicclassPet {
}
That's it, a class is defined. Of course, it is empty, so let's give it some properties to work with:
JAVA
publicclassPet {
String name;
int weight;
}
Great, now that we have two properties that make up a pet, let's create objects using this class.
Creating an Object
Once we have a class defined, creating an object is easy, just use the new keyword, like this:
JAVA
Petpet=newPet();
We now have an object of type Pet. We still haven't actually been able to set the values of name and weight just yet, so let's do that now.
Using a Constructor
A constructor is a special method that literally constructs an object given the parameters you pass in. If all we want to do is set the value of name and weight, this is how the class would look like:
JAVA
publicclassPet {
String name;
int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
}
The constructor takes in the parameters needed to build the object, initializes the values it needs to, and then returns an instance of that class which essentially is now a separate object.
Now you can create a new Pet object like this:
JAVA
Petpet=newPet("Scooby", 4);
Let's throw a method in this class so we can see our objects in action.
Because classes are just blueprints for objects, we can create multiple objects without any interference between each other. Let's create multiple pets to illustrate this: