Call method with parameter of another method in Java? [closed]

-2

I would like to call a method with a parameter being an object of another class, but is this object created in an earlier method?

Example:

Class A:

Public void cadastrar (Cliente cliente){
      System.out.println (cliente.getNome());

}

ClassB:

Public void metodoDois (){

 Cliente cliente = new Cliente();

}

ClasseA chamada = new classeA();
    Chamada.cadastrar(cliente);
    
asked by anonymous 19.04.2018 / 02:40

1 answer

0

As it is not possible

You can change the metodoDois() and pass it by parameter to the cadastrar() method:

Class A:

Public void cadastrar (Cliente cliente){
    System.out.println (cliente.getNome());
}

ClassB:

Public Cliente metodoDois() {
    return new Cliente();
}

ClasseA chamada = new classeA();
Chamada.cadastrar(metodoDois());

Or create a variable outside the function metodoDois() that will be modified in the function call and then pass as a parameter of cadastrar() :

Class A:

Public void cadastrar (Cliente cliente){
    System.out.println (cliente.getNome());
}

ClassB:

Cliente cliente;

Public void metodoDois() {
    this.cliente = new Cliente();
}

ClasseA chamada = new classeA();
metodoDois();
Chamada.cadastrar(cliente);
    
19.04.2018 / 03:31