The Dot (.) Operator in Java

Rashmi Patidar Nov 22, 2021
The Dot (.) Operator in Java

In Java language, the dot operator (.) symbolizes the element or operator that works over the syntax. It is often known as a separator, dot, and period. Simply the dot operator acts as an access provider for objects and classes. The usage of the above operator is as below.

  1. It separates a function and variable from an instance variable.
  2. It allows to access sub-packages and classes from a package.
  3. It leads to access the member of a class or a package.
public class DotOperator {
    void show() {
        int i = 67;
        System.out.println("In show method: "+ i);
    }

    static boolean isGreater(int a, int b) {
        return a > b;
    }

    public static void main(String args[]) {
        DotOperator doe = new DotOperator();
        doe.show();
        System.out.println("Is 5>4: " + DotOperator.isGreater(5, 4));
    }
}

In the above code block, the use of the instance method and static method gets showcased. The code block has a public DotOperator class that has two member methods. The internal working of the member method show is to display a local instance variable using print stream.

The class holds another static isGreater() method that takes two parameters. The result of the operation is a boolean value if the two inputs are greater or lesser than each other.

Lastly, the class holds the main method, which tracks the actual logic to perform usage of dot operator. In the main function, an instance of parent class that is DotOperator gets created.

The instance variable now used to access the class’s member function show. The method calls the show function and displays the value that gets initialized and instantiated in the class.

Similarly, the class name DotOperator gets directly allowed to access the static method of the DotOperator class. The function also returns true or false based on the first value being more than the second one.

The boolean output gets returned and printed in the main method of the class.

Below is the output of the above code block.

In show method: 67
Is 5>4: true
Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

Related Article - Java Operator