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.
- How to Install Node on Windows, macOS and Linux
- Getting Started with Svelte
- Create an RSS Reader in Node
- Getting Started with Electron
- Best Visual Studio Code Extensions for 2022
- Getting Started with Deno
- How to deploy an Express app using Docker
- Getting Started with Sass
- Learn how to use v-model with a custom Vue component
- Using Puppeteer and Jest for End-to-End Testing
- Learn how to build a Slack Bot using Node.js
- Getting Started with Vuex: Managing State in Vue