In our previous example, we had a class with a default constructor where we passed no arguments into the class. That is great, unless you want to set one or more class variables with data at run time. So lets look at overloading the class constructor.
public class Greeting {
private int location;
private String message;
public Greeting() {
this.location = 3;
this.message = "Welcome to Earth.";
}
// here we overload the constructor in case our message
// needs to be different
public Greeting(String newMsg) {
this.location = 3;
this.message = newMsg;
}
}
In this example, we keep our default constructor, but notice we’ve added another constructor, in which we change the default message.
We can have as many different constructors as we want by overloading them, just as if we overloaded a method. The only rule is to overload a constructor (or method) is that you have to have different parameters to pass. That means you cannot have two constructors with only a single string, as the system couldn’t determine which to you.
Now let’s look at what if we wanted to add a function or two.
Getters, Setters, and other Methods
We’ve seen some simple methods before, but let’s look at some common ones. Specifically getters and setters.
Since we will usually have our class level variables private, we need to get and set their values with a method call. Let’s look at some samples that would be in our class.
public int getLocation() {
return this.location;
}
You will notice this is very simple, returning the value. But why not just have access to the variable? It’s because you cannot have only read access, and not write. So if you can read the variable directly (because it’s public for example) then you can also write it. This can cause issues if someone sets a bad value. The setter can protect us from bad values. Consider the following function.
public void setLocation(int value) {
// the location can only be between 1 and 9
if(value <= 0 ) {
this.location = 1;
} else if (value >= 10) {
this.location = 9;
} else {
this.location = value;
}
}
Notice how we check to make sure the value passed in is proper. We use basic error checking to ensure that bad data is corrected. We could also ignore that bad data, throw an exception, or perform a different task if we wanted to.
Methods are not limited to getting and setting however. We can write other methods that can be used to simplify common tasks. Consider the following.
public void incrementLocation() {
this.setLocation(this.getLocation() + 1);
}
This method is designed to allow the programmer to simply add one to the value of the location, without even having to know what the current value is. Let’s look at how it works.
This method should add one to the current location value. However, it does so with the other methods. First it gets the value of the location via the getLocation() method, and adds one to that value. That gets passed as a parameter to the setLocation() method. From there, the value is evaluated to make sure the date is good.
Every class in Java extends the default Object class. This gives everyone some basic default methods and constructors. However, we can override, or replace if you would prefer, any of these methods.
A common method to override is the toString() method. It lets us convert our object to a string. We can put anything we want in there. By default you’ll get a Class name and a memory address. However, we’re going to make it more relevant to us.
public String toString() {
return this.message;
}
Intro to Java Classes – Getters, Setters, and other Methods was originally found on Access 2 Learn