Using this in Java

Asked

Viewed 66 times

2

Hello, in the code below, I just put this in the size attribute because it has a parameter with the same name as it, so that the compiler does not get confused. But I doubt if I put myself in others too.

public class Pilha {
    private int tamanho;
    private Object[] vetor;
    private int topo;

public Pilha(int tamanho) {
    this.tamanho = tamanho;
    vetor = new Object[this.tamanho];
    topo = -1;
}

I always see my teachers referencing all class attributes, in all methods, with this, whether or not there is a local variable with the same name that can make the compiler misunderstand. If there is no such variable, the compiler no longer understands that these attributes refer to those of the class without using this?

I want to understand mainly how the compiler works when reading this code. If for him it is better to use this even with nothing that can confuse him or not.

In case you have any good material on this kind of subject, I am accepting indications.

No answers

Browser other questions tagged

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