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;
Syntax to access properties or methods of an object/class?
– rray
Yeah, but I think there was something about the operator or something.
– rubStackOverflow
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)– Wallace Maxters
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.
– Ivan Ferrer
Actually in java, pointers are called references. Only in java there is no pointer arithmetic.
– Ivan Ferrer