What is the name of this part of the code?

1

Let's say I create a class ...

    public class Teste1 {

        public void Teste1(){
            System.out.println("Olá");
        }

        public void teste2(){
            System.out.println("Oi");
        }

    }

Teste1 is the constructor method of the class.

And what is the Teste2 called? Is it also a constructor method? What is the name of this part of the code ( Teste2 )?

    
asked by anonymous 11.04.2017 / 23:28

1 answer

5

Method. Just this. Specifically it is an instance method, but usually we do not need to specify fully.

The constructor is only what has the same name of the class and has no return, and that in theory should build something, ie assign values to the attributes of the class or do something at startup. So there are no constructors in your class, at least not explicitly. There's even a default implicit builder .

class HelloWorld {
    public static void main(String[] args) { 
        Teste1 teste = new Teste1(); //criando a classe com um construtor implícito
        System.out.println("Criou a classe");
        teste.Teste1(); //está chamando o método normal declarado
    }
}

class Teste1 {
    public void Teste1() { //este método não é um construtor
        System.out.println("Olá");
    }

    public void Teste2() {
        System.out.println("Oi");
    }
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
11.04.2017 / 23:42