Chapter 1 - Java for Beginners Course

Accessing class members - The dot operator

In the previous examples, you may have seen that we’ve used the dot (.) operator in statements like:

plane1.setNumberOfSeats(270);

In Java, we use the dot operator to access fields or invoke methods that belong to an object or a class.

Here, we are invoking the method setNumberOfSeats of the Airplane object that is referenced by plane1.

Let’s look at another example:

Defining our example class

Door class

//...
public class Door {
    public boolean locked = false;

    public void lockDoor() {
        locked = true;
    }

    public void unlockDoor() {
        locked = false;
    }
}
Note that against best practices, we are marking the locked field as public. This is for the purpose of the example only.

Accessing class members

Let’s assume that we have a door variable and that we assign a new Door instance to it, like this:

Door door = new Door();

When we have an object reference, like door, we can make use of the dot operator to access the members that are defined in the Door class.

We can perform two main types of actions:

Invoking methods

The Door class defines 2 methods, lockDoor and unlockDoor.

To invoke methods in an object when we have an object reference, we specify the name of the reference, followed by the dot operator and the name of the method we want to invoke. We also need to provide any input parameters inside parenthesis.

For example, to invoke the lockDoor method:

door.lockDoor();

Accessing fields

The Door class defines 1 field, locked that is a boolean value used to store whether the door is locked (true) or not (false).

To access a field, we also follow the same approach as when invoking a method, that is, name of the reference, followed by the dot operator, and then the name of the field we want to access, for example:

boolean isLocked = door.locked;

Here we are reading the current value of the locked field of the door object and assigning it to a new boolean variable called isLocked.

We can also modify the value of the field, for example:

door.locked = true;

DoorApp class

If you want to give this a try, we’ve put the example together in the DoorApp class. You can find the code for both classes following the links under the Code in GitHub section.

//...
public class DoorApp {

    public static void main(String[] args) {
        Door door = new Door();

        // invoking methods
        door.lockDoor();

        door.unlockDoor();

        // accessing fields (read)
        boolean isDoorLocked = door.locked;

        // accessing fields (write)
        door.locked = true;

        System.out.println("isDoorLocked variable = " + isDoorLocked);
        System.out.println("door.locked field = " + door.locked);
    }

}

Output:

isDoorLocked variable = false
door.locked field = true

Appendix: Invoking methods on the same object

An exception to the above is when you are invoking a method that is defined in the same object, for example, assume we add a new method to our Door class called setDoorLock that allows us to change whether our door is locked or not:

public void setDoorLock(boolean lockValue) {
    locked = lockValue;
}

We can then use that method from inside our lockDoor and unlockDoor methods directly without needing to use the dot operator, for example:

public void lockDoor() {
    setDoorLock(true);
}

public void unlockDoor() {
    setDoorLock(false);
}

In this particular example, both approaches have the same end result.

There are some common pitfalls around this that will be covered when the this keyword is introduced in chapter 5. We’ll also go into more detail about what we mean with same object.