Classes and Objects
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
:
JAVApublic class Pet {
}
That's it, a class is defined. Of course, it is empty, so let's give it some properties to work with:
JAVApublic class Pet {
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:
JAVAPet pet = new Pet();
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:
JAVApublic class Pet {
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:
JAVAPet pet = new Pet("Scooby", 4);
Let's throw a method in this class so we can see our objects in action.
JAVApublic class Main {
public static class Pet {
String name;
int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
void printInfo() {
System.out.println("Name: " + name + " Weight: " + weight + " pounds.");
}
}
public static void main(String[] args) {
Pet pet = new Pet("Scooby", 4);
pet.printInfo();
}
}
BASHName: Scooby Weight: 4 pounds.
Multiple Objects
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:
JAVApublic class Main {
public static class Pet {
String name;
int weight;
Pet(String name, int weight) {
this.name = name;
this.weight = weight;
}
void printInfo() {
System.out.println("Name: " + name + " Weight: " + weight + " pounds.");
}
}
public static void main(String[] args) {
Pet scooby = new Pet("Scooby", 4);
scooby.printInfo();
Pet spark = new Pet("Spark", 6);
spark.printInfo();
}
}
BASHName: Scooby Weight: 4 pounds.
Name: Spark Weight: 6 pounds.
- Managing PHP Dependencies with Composer
- Getting Started with Svelte
- Getting Started with Express
- How to Serve Static Files with Nginx and Docker
- How to build a Discord bot using TypeScript
- How to deploy a Deno app using Docker
- How to deploy an Express app using Docker
- How to deploy a Node app using Docker
- Getting Started with Sass
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Using Axios to Pull Data from a REST API
- How To Create a Modal Popup Box with CSS and JavaScript