Call another class method

Asked

Viewed 2,152 times

0

I am beginner in java, and I am creating a basic CRUD of a library, as I have many operations, I thought better to make a specialized class for each operation, which will call the methods of the library class. I am doing the registration operation class, where it will call the methods registerAuthor, register, register.

but I don’t know what to call the methods, because they are in a different class.

follows the code:

Library class

public void cadastarAutor(Autor autor) {
    this.autores.add(autor); 


}

public void cadastrarEditora(Editora editora) {
    this.editora.add(editora);

}

public void cadastrarLivro(Livro livro) {
    this.livro.add(livro);
}

Class Cadastrar

import RepositorioLivros.Biblioteca;

  public class Cadastrar {

    Biblioteca biblioteca= new Biblioteca();

    biblioteca.

}

how do I call the library class methods to use in the Register class? , I set up a library in the register class and have tried to do so: library. and call the methods, but when I try to call ñ works, the Eclipse doesn’t even show the options after the .(dot)

The correct one would be when I tried to call, appear the options of the methods that are in the library class, but Ñ appears, after I type library. , he makes a mistake: inserir a descrição da imagem aqui

  • What happens when you try to call?

  • I edited and put the bug image in my question, update and review

  • import RepositorioLivros.Biblioteca;: has a package called RepositorioLivros?

  • yes Igor Venturelli has yes

  • 1

    Registration is to be a class or a method?

  • Which package to class Cadastrar is?

  • Edit the question including the complete code of the two classes, please. From the first line of the file to the last

  • As I said, I made a separate class for each operation, the operation class will only handle the operations of registering, but the operations are in the Library class, and I want to call them to the Register class.... The Register class is in a package called Operations

Show 3 more comments

1 answer

4


The class body may contain only statements by class members, such as

  • fields
  • interfaces
  • methods
  • instance initializers
  • static initializers
  • manufacturer’s declarations for the class

To call some class method Biblioteca, define a method within the class Cadastrar and use the Biblioteca together with the point rating.

public class Cadastrar {
    // ...
    public void meuMetodo() {
        biblioteca.cadastrarAutor(...);
    }
}

Read more about the class body in Java here.

Browser other questions tagged

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