What is and what is the function of . (dot) in POO?

Asked

Viewed 1,072 times

7

A long time ago a teacher defined and explained the function of . in POO (Java class).

With time I ended up forgetting, but I found it quite interesting, often we think is just a "point", however, it is not quite so.

So the question: O que é e qual a função . (ponto) em POO?

  • 2

    Syntax to access properties or methods of an object/class?

  • Yeah, but I think there was something about the operator or something.

  • 1

    This can vary from one language to another. For example, in PHP you use the arrow operator ->, javascript can optionally use the ['nome_do_atributo']. That answer will be variable. But in the context requested by the question, it is an operator that will allow you to access the members of a class instance (the object)

  • I think your teacher must have talked about pointers (pointers), not point. Pointer has a great importance within classes, methods etc., it serves to call attributes, methods belonging to class or attribute, or the current scope. And in the case of Java, if I’m not mistaken, the dot is used as a pointer.

  • Actually in java, pointers are called references. Only in java there is no pointer arithmetic.

3 answers

12


It is a binary operator (has two operands, one on the left and one on the right side). Usually called Operator.

The most common is that on the left side is the object to which you are referring. On the right side is the message that is passing to the object. The most common is to be the invocation of a method, but it can also be the direct access to a variable of the object. So it is the access operator to members of an object. There is a case that the left side is not the object but some member that gives access to an object.

There are cases where the access is not done in the object but directly in the class.

You can also use it as a package name separator, but that’s not so much related to the focus of the question.

Documentation.

7

You mean point operator (dot Operator)?

According to the Java Tutorial available in this link. The point serves to:

Code that is Outside the Object’s class must use an Object Reference or Expression, Followed by the dot (.) Operator, Followed by a simple field name...

In free translation:

Code outside the object needs to use an object reference or expression, followed by the point operator...

I hope I’ve helped.

  • 1

    This is more related to how external access is given from within a class.

3

That operator is also referred to as Operator.

As @Maniero’s response already speaks, he is an operator binary (has 2 operands in the form a.b) who accesses the members of an object or class as follows:

I have a class Lampada:

public class Lampada {
    public boolean acessa = false;

    public void acender() { acessa = true; }
    public void apagar() { acessa = false; }
}

Note that this code does not follow any kind of "good practices" of programming, it is just an example.

The members of Lampada are the field private acessa and the methods ligar() and desligar().

Realize that all this is inside a scope, that means that we can’t access something within that scope outside of it, that’s where the Operator comes to save the homeland. Having an instance of this class we can access its members:

public static void main(String[] args) {
    var lampada = new Lampada(); // Instância da Lampada

    boolean acessa = lampada.acessa; // Acessando o campo 'acessa'
    lampada.ligar(); // Ao acessar métodos eles são executados, isto é:

    System.out.println(lampada.acessa); // true
    System.out.println(acessa); // false
}

Also note that using the point we can also separate the scopes, that is to say, c is different from a.c which is different from a.b.c. In the latter case we are accessing a scope within a scope within a scope:

class Exemplo {
    class A { public B b = new B(); }

    class B { public C c = new C(); }

    class C {}

    public static void main(String[] args) {
        var a = new A();
        a.b; // Acessa 'b' dentro da classe A
        a.b.c; // Acessa 'c' dentro da classe B que está dentro da classe A
    }
}

Note that this is exactly what happens when we call System.out.println(), but instead of a.b.c be a variable, is a variable method out, which is an instance of java.io.PrintStream. See how this operator really is important and very used!


The dot is also used to separate the "domains" of a package:

package da.up.na.resposta;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.