Another in the Java Basics category, this article goes over a simple use of initializing data in a class via a constructor. There’s a couple elements to consider, such as using getters and setters, using “this” to assign values and whether or not to call the getters and setters from the initialization part of the class.

Getters & Setters

A common approach is to set up a class using all private fields, and then adding in public getters and setters. Here’s an example:

public class Account {

    private int accountNumber;
    private double balance;
    private String customerName;
    private String email;
    private String phoneNumber;

    public int getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(int accountNumber) {
        this.accountNumber = accountNumber;
    }

In the above code snippet, we have a series of fields, and each has it’s own getter and setter. This allows a class outside to reference this private data.

Example: We can’t directly call accountNumber. So we create a getter and setter for it. These are public methods that can be accessed via a constructor like so:

Account briansAccount = new Account();
briansAccount.setAccountNumber(55728);
System.out.println(briansAccount.getAccountNumber);

It makes sense… we hide the fields, and then create public methods that expose limited access (such as setting a value or getting the value of that field.)

Inheritance Problems

These getters and setters become a problem when we get into sub-classing or inheritance.

Initializing The Class Fields

Rather than calling all those getters & setters one by one, we can setup the class with an initialization method, like so:

public class Account {

    private int accountNumber;
    private double balance;
    private String customerName;
    private String email;
    private String phoneNumber;

    /*
        Below is a constructor that initializes the class with specific field data:
        Notice that the constructor is within the main class definition.
     */

    public Account(int accountNumber, double balance, String customerName, String email, String phoneNumber){
        this.accountNumber = accountNumber;
        this.balance = balance;
        this.customerName = customerName;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }

In the code snippet above, we introduce a method for the Account class. The method is also called Account, and resides within the class. Here we set the fields equal to their param values passed in.

In this way, when we instantiate a the class, we can pass in all the values at instantiation:

Account briansAccount = new Account(887363, 9827.00, "Brian Warner", "brian@sdet.us", "8887772231");

Initializing with This

In the initialization we could have called the setters and getters. However, there’s some opinion as to calling methods from within initialization. They way I was taught, was to avoid doing so.

So rather than using Setters and Getters, the initialization sets up the instantiation via the “this” keyword.

The keyword “this” references the fields of the class. By writing this.accountNumber = accountNumber; in the above code snippet, we are setting the field of the class equal to the value of passed in during the object creation (class instantiation.)

The getters and setters can still be called. They aren’t completely omitted, they are just not used in the initialization.

Default Values

Let’s say you want to allow instantiation of the Account class, without any parameters. This can be done using a method overload like so:

public class Account {

    private int accountNumber;
    private double balance;
    private String customerName;
    private String email;
    private String phoneNumber;
    
    public Account(){
        this(00000, 0.00, "TEST Name", "test@email.com", "088088088088");
        System.out.println("Dummy data is used on creation of empty Account class....");
    }

    public Account(int accountNumber, double balance, String customerName, String email, String phoneNumber){
        this.accountNumber = accountNumber;
        this.balance = balance;
        this.customerName = customerName;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }

In the above case, the Account method is overloaded (rendered twice. This allows an empty Account to be instantiated like so: Account demoData = new Account(); That will create an account with account number 00000.

Overloading could be done in such a way that some fields are provided and some are set with default values (i.e. supporting only accountNumber and balance, and defaulting values for email and phoneNumber) in the overloaded method.

IntelliJ Shortcuts

IntelliJ offers some shortcuts to set up Constructors and Getters and Setters.

In the IntelliJ IDE, if you right click a class field, a contextual menu will appear.

From here we could chose “Constructor.” When we chose Constructor, we’ll get the following sub menu options, prompting a selection of fields to build constructors from:

When the field(s) are selected and committed, IntelliJ will create a single constructor with those fields as parameters.

Similarly with Getters & Setters, if you right-click a field definition in a class, we get the same contextual menu. From here we can choose to create Getters, Setters or Getters & Setters.

Once selected, we’ll get another menu asking us which fields we want to create Getters, Setters (or both) for. Again, selected (or multi-selecting) the fields will create the getters and setters for the required fields.

Leave a Reply

Your email address will not be published. Required fields are marked *