Sitemap
Jack Boyuan Xu

Jack Boyuan Xu

Co-founder & Tech Lead @ EthSign. Blockchain Lecturer @ USC.

[JAVA-2h] Getters, Setters, Constructors

3 min readJan 14, 2020

--

We just had a baby! Yes, you and I just had a baby! Let’s not get bogged down by who’s the father, who’s the mother, or how we conceived it in the first place as these are just minor details. One of the most important things when it comes to a human being is blood type. The blood type is determined at birth (or maybe when the embryo was developing, but let’s not get bogged down by this minor detail) and never changes throughout someone’s life.

Getters and Setters

If we want to turn all of the above into code, we can start by creating a Human class. We don’t want anyone to modify the person’s blood type, so we can make the blood type variable private. However, obviously we still want to read it. We can setup something like this:

class Human {
private String bloodType;

// This method below is called a “getter” because it gets the private variable.
// A “setter” sets the private variable instead by taking in a parameter.
// Why not make the variable public then? Because with a getter and setter, we have more control. For example, with a setter we can impose restrictions on what values can be set to the variable.
// In this case, we only have a getter because we don’t want anyone else to set the blood type.

public String getBloodType() {
return bloodType;
}
}

But how can we only allow setting the blood type in the very beginning? Enter constructors.

Constructor

A constructor is a method called when the object is made, or constructed. Recall how we instantiate an object:

Get Jack Boyuan Xu’s stories in your inbox

Join Medium for free to get updates from this writer.

Human h1 = new Human();

Last time we said the last part of this line, Human(), looks like it’s calling a method, and indeed it is. Precisely, it’s the constructor that’s being called. When we don’t explicitly define a constructor, Java will use a default constructor that doesn’t really do anything, therefore our code still works without us giving it a constructor. However, let’s go ahead and give it a constructor:

public Human() {
System.out.println(“I am not a lizard.”);
}

It is a special method with no return type and its name is the name of the class. If we add the constructor into the class and instantiate the object in the main method, we will see I am not a lizard. printed in the console.

Press enter or click to view image in full size

Even though it’s special, it is still a method, which means we can make it take in parameters. Now we have our way to set the blood type during instantiation!

Press enter or click to view image in full size

Awesome! Now we can harness the power of constructors and initialize values in an object. In the next article, we will return to our beloved simple calculator for some final modifications to make it more bulletproof and user friendly.

--

--

Written by Jack Boyuan Xu

Co-founder & Tech Lead @ EthSign. Blockchain Lecturer @ USC.