-17
RequestSpecification requestSpecification = new RequestSpecBuilder().
            addHeader("Content-Type", "application/json").
            addHeader("Accept", "application/json").
            build();

As in the above statement what does the dot(.) represent. Like after each call to a method and if we press dot (.) and then control space in Eclipse it populates/suggests the method under it. Can someone explain what does each dot (.) represent. Is that calling a method part of a class using an object? It is big confusing for me. Please explain.

M06494h
  • 146
  • 1
  • 1
  • 13
  • 2
    it means that you are chaining your statements. String a = myObject.getValue().toString(); is the same as: MyObject m = myObject.getValue(); String a = m.toString(); – Stultuske Oct 02 '19 at 11:38
  • 4
    In Java, the dot marks access to a sub-item of one identifier to another identifier. It's one of the basic syntax concepts of Java. – Elias Oct 02 '19 at 11:41
  • 2
    Dot operator lets you access members of objects (fields, methods, inner types). Methods can return a value like `getName()` can return a String object like `String name = employee.getName();` and on that returned object you can call its own methods like for String charAt: `char firstChar = name.charAt(0);`. You can chain those methods like `char firstChar = employee.getName().charAt(0);`. For your specific case research for *builder* pattern. – Pshemo Oct 02 '19 at 11:41
  • Possible duplicate of [how to achieve method chaining in java?](https://stackoverflow.com/questions/21180269/how-to-achieve-method-chaining-in-java) – Elias Oct 02 '19 at 11:42
  • 2
    It is much more common for the `.` to appear after line breaks rather than before. It is easy to miss a dot at the end of the line, so the next line looks like the start of a statement, rather than the continuation of an expression. – Andy Turner Oct 02 '19 at 11:45
  • Possible duplicate of [Java DOT operator, What it actually does](https://stackoverflow.com/questions/37646364/java-dot-operator-what-it-actually-does) – vaibhav shanker Oct 02 '19 at 11:51
  • Thanks for your responses. Now I understand what the dot (.) is doing. – M06494h Oct 02 '19 at 12:06
  • 3
    BTW, despite some comments, dot '.' is **not** an operator, it is a **separator**: Java Language Specification [3.11. Separators](https://docs.oracle.com/javase/specs/jls/se13/html/jls-3.html#jls-3.11) and [3.12. Operators](https://docs.oracle.com/javase/specs/jls/se13/html/jls-3.html#jls-3.12). Normally used in [15.11. Field Access Expressions](https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.11) and [15.12. Method Invocation Expressions](https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.12) – user85421 Oct 02 '19 at 12:50

3 Answers3

1

Using Dot (.) operator with an Object, you can access its methods and properties. In general, with a Dot Operator, you can access the members of a package or class.

For Example:

class Student{
    private int roll;

    public void setRoll(int r)
    {
        this.roll=r;
    }

    public int getRoll(){
        return this.roll;
    }
}


class UseStudent{
    public static void main(String []args)
    {
        Student s=new Student();
        s.setRoll(101); //Accessing roll method of Student using Dot operator
        System.out.println(s.getRoll()); 
    }
}
Gary
  • 13,303
  • 18
  • 49
  • 71
0

In general: When you dot after an object, you can call any method or property available in that object.

Example: You have a class Human with properties: int Age, string Name, and method: sing() After creating an Human object you will be able to call its properties or methods such as: human.sing() or human.Name

In this specific case: A RequestSpecBuilder has a method (as you see) called addHeader(). this addHeader returns a RequestSpecBuilder, which allows you to do multiple .addHeader(). enter image description here

Duarte Castanho
  • 305
  • 2
  • 6
  • 20
-3

The dot (.) represents how to access the public methods of an object. Chaining of the methods basically means that this method also return the same object that is being instantiated or reference.

Gideon
  • 71
  • 5