Call another class method

0

I'm a beginner in java, and I'm creating a basic CRUD from a library, as I have a lot of operations, I think it's best to do a specialized class for each operation, which will call the methods of the library class. I'm doing the class of the operation register, where it will have to call the methods registerAuthor, registerBook, registerAuthor of the library class.

but I do not know how to call the methods because they are in a different class.

follow the code:

Class Library

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 Join

import RepositorioLivros.Biblioteca;

  public class Cadastrar {

    Biblioteca biblioteca= new Biblioteca();

    biblioteca.

}

How do I call the methods of the library class to use in the class Register ?, I instantiated a library in the class register and already tried to do this: library. and call the methods, but when I try Eclipse does not even show the options after . (dot)

The correct would be when I tried to call, the options of the methods that are in the library class appear, but Ñ appears, after I type library. , it gives an error:

    
asked by anonymous 29.06.2017 / 18:50

1 answer

4

The body of the class can contain only class member declarations, such as

  • fields
  • interfaces
  • methods
  • instance initializers
  • static initializers
  • constructor declarations for class

To call any method of the Biblioteca class, define a method within the Cadastrar class and use the Biblioteca instance together with the point notation.

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

Read more about the class body in Java here .

    
29.06.2017 / 19:06