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 in more detail about what we mean with same object.
Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Main class for this step
Dependencies

This is a list of recommended tutorials or courses that might be useful before starting this one.

Contents
Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations