How to call builders with arguments?

Asked

Viewed 1,282 times

3

How to define constructor values in the main class by Scanner in such a builder?.

public Aluno(String nome,int idade) {
 this.nome = nome;
this.idade = idade;
}

and in the main class call:

Aluno pedro = new aluno();

and insert the data into the main class by Scanner.

  • I didn’t understand the doubt, you must [Dit] the question and put more relevant information so we can help you. Maybe improve the example of what you want or just explain better what you need.

  • Ali no em aluno pedro = new Aluno(); I need to pass the data obligatorily, in the example that I had seen it set in the main class scanner. how can I do?

  • You can tell if this is correct http://pastebin.com/XpEh0Qb0

  • Pedro, when asked to take a question here in the comments, you should [Edit] to the question, it’s not trouble, it’s important. Thank you!

1 answer

1


The code is right but could do better:

class Aluno {
    String nome;
    int idade;
    public Aluno(String nome, int idade) {
        this.nome = nome;
        this.idade = idade;
    }
    public Aluno() { }
}
 
class Principal {
    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        Aluno aluno = new Aluno();
        System.out.println("Qual tu nome");
        aluno.nome = entrada.nextLine();
        System.out.println("Qual tu idade");
        aluno.idade = entrada.nextInt();
        System.out.println("Nome: " + aluno.nome + "\nIdade: " + aluno.idade);   
    }
}

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

I could ask if I should have the constructor without parameters. In real code, it might not be a good idea, it is not ideal to create an object in an invalid state. But in an exercise it is no problem, even more than the solution being given was to create an object with invalid state "by force".

If you didn’t have the constructor with parameters, then you wouldn’t need any constructor, after all, when you don’t have constructors, Java creates a default without parameters that doesn’t perform initializations.

See more about builders.

  • Exactly what I was looking for, now I managed to understand better, however, still I would like to ask... Can’t I just use a constructor with parameter? Why do I have to use the constructor without parameter as well?

  • You may not have it and even I would recommend doing this. I only kept it to keep the same semantics that he used. When not constructors, the constructor without parameters is automatically created by the compiler to build the object. When you create any constructor, it is no longer necessary and is not created. I think ideal lies this is desirable because it prevents you from creating an object with potentially invalid state. Of course there is a situation that this is desirable, or the constructor without parameters can create an object in a valid way (this is not the case). The way up was better not to have at all.

  • But this is just a basic exercise. The intention is not to do the right thing for a real system. He is only learning basic concepts, better not complicate. The exercise would be totally different if only had the constructor with arguments.

  • Oh understood, I just asked because I’m learning the basics too, and all the exercises I’m trying to do with a constructor with parameter, he gives a message and "forces" me to put the constructor without parameter too, in this case the message goes away... rs But I understood yes, Thanks @bigown

  • Give what message? It is not to be required. Also because if it were, the compiler could solve this.

  • I get it, I... So I have a main class called Principal5 and a class called Calculator, in Calculator I created a constructor with parameters. And when I go to instantiate the Calculator class in the main class, in the instance line, it appears "Create constructor "Calculator()" in principal5.Calculator" And the project gets an error icon in Principal5.java

  • This must be a bad thing for the IDE you are using. Ideally, disable this.

  • Oh I get it, I’ll disable it here then. Thank you.

Show 3 more comments

Browser other questions tagged

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