What’s that part of the code called?

Asked

Viewed 193 times

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");
        }

    }

The Teste1 is the constructor method of the class.

And what’s called the Teste2? Is it also a constructor method? What name is given to that part of the code (Teste2)?

  • 2

    It’s just a common method, no big deal. And Teste1 He is not a builder, because builders have no return. The only thing that’s different is that you’re not following the method names rule of being Camelcase, and start with minuscule letter.

  • So I’m getting it all wrong even the

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

5

Method. That’s it. Specifically it’s an instance method, but normally we don’t need to specify it completely.

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

class Main {
    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");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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