Journey to Java: Episode 7 “OOP Continued and the Constructor”

In the last entry to the series of me documenting my experience learning Java as a new language we learned about classes, objects, fields, getter and setter methods, and more. There is a lot of concepts and syntax to remember here so it is important to code out some projects to practice. This entry will be a continuation of the last, touching on the topic of object oriented programming in Java.

Before I get into new topics I wanted to highlight a shortcut for creating getters and setters using an IDE like Eclipse to save time writing all of that code. While writing getters and setters take time to write out, it is important to understand what is happening under the hood before you use a shortcut. If you are using Eclipse, after you write out all of your fields in the class you can go to “Source” in the top bar and select “Generate Getters and Setters”. Other code editors have options for this as well so be sure to look up your IDE and see what options are available to you.

Constructor

Just as there is a quicker way to type out all of your getter and setter methods, Java has an easier way to create fields in a class. This is where the constructor comes in. A constructor is used to create fields upon initialization of a class. Java already makes a constructor when you use the new keyword calling the constructor method.

(BaseballTeam orioles = new BaseballTeam(); 

To make our own constructor method we need to go into our class of choice, I will be using BaseballTeam. The constructor is a method that is called once upon creating a new instance of the class. It needs to be named the exact same as the class and only needs an access modifier.

public class BaseballTeam {   public BaseballTeam() {
System.out.println("we are in the constructor")
}
}

At this point we are able to start filling in fields using parameters in the constructor.

public class BaseballTeam {   public BaseballTeam(String city, String name, 
String colorOne,String colorTwo) {
this.city = city;
this.name = name;
this.colorOne = colorOne;
this.colorTwo = colorTwo;
}}

Now that our constructor is set up to take on some parameters and assign those to fields, we can appropriately call our constructor this time. Again, to call the constructor we need use the new keyword and pass in the values we wish to initialize our instance with.

//inside main
BaseballTeam orioles = new BaseballTeam("Baltimore", "Orioles", "Orange", "Black")

With this code in place our fields are being updated upon initialization of a new object.

Another thing to note is that you can overload constructor methods. You can have multiple constructors as an example of polymorphism. You achieve this by having multiple of the same methods but different ones are called based on the parameters given. A use case for this would be to have an empty constructor to set default values if no arguments are given, or a full constructor to assign fields. To achieve this we would need a special way to use the this keyword.

public class BaseballTeam {   public BaseballTeam() {
this("No city", "no mascot", "no color", "no color");
}}

We now know how the constructor is working under the hood assigning values to fields upon initializing of the object. We could refactor the above code to incorporate the setter methods but because of something we will learn about later called sub-classing and inheritance it may not work. It is generally best practice to assign the values of fields directly for this reason.

//best practice
public class BaseballTeam {
public BaseballTeam(String city, String name,
String colorOne,String colorTwo) {
this.city = city;
this.name = name;
this.colorOne = colorOne;
this.colorTwo = colorTwo;
}}//works too but may cause issuespublic class BaseballTeam {public BaseballTeam(String city, String name,
String colorOne,String colorTwo) {
this.setCity(city);
this.setName(name);
this.setColorOne(colorOne);
this.setColorTwo(colorTwo);
}}