Home  >  Article  >  Java  >  Package Access Example

Package Access Example

Patricia Arquette
Patricia ArquetteOriginal
2024-09-29 06:16:01526browse

Exemplo de Acesso a Pacote

In the example presented, the classes Book and BookDemo belonged to the same package, allowing BookDemo to access Book without problems, as standard access grants visibility to all members within the same package.

However, if the Book and BookDemo classes were in different packages, BookDemo would not be able to access Book. To allow the use of Book from other packages, three changes must be made to the Book class:

  • Declare the Book class as public: this makes the class accessible outside the bookpack package.
  • Make the Book constructor public: this will allow you to create Book instances in other packages.
  • Declare the show() method as public: this way, it can be invoked outside the bookpack package.

Code Example: Book Made Public

  1. Defining the Book Class in the bookpack Package (file: Book.java)
package bookpack;

// Agora a classe Book é pública para permitir o acesso a partir de outros pacotes.
public class Book {
    private String title;
    private String author;
    private int pubDate;

    // Construtor público
    public Book(String t, String a, int d) {
        title = t;
        author = a;
        pubDate = d;
    }

    // Método público para exibir os detalhes do livro
    public void show() {
        System.out.println(title);
        System.out.println(author);
        System.out.println(pubDate);
        System.out.println();
    }
}

Example: Accessing Book from Another Package

  1. Using Book from the bookpackext Package (file: UseBook.java) In this example, UseBook is a class located in a different package (bookpackext), and to use Book, the class name needs to be qualified with the full package name (bookpack.Book).
// Esta classe está no pacote bookpackext.
package bookpackext;

// Usa a classe Book do pacote bookpack.
class UseBook {
    public static void main(String args[]) {
        // Qualifica a classe Book com o nome do pacote para acessá-la.
        bookpack.Book books[] = new bookpack.Book[5];

        // Cria uma lista de livros
        books[0] = new bookpack.Book("Java: A Beginner's Guide", "Schildt", 2014);
        books[1] = new bookpack.Book("Java: The Complete Reference", "Schildt", 2014);
        books[2] = new bookpack.Book("The Art of Java", "Schildt and Holmes", 2003);
        books[3] = new bookpack.Book("Red Storm Rising", "Clancy", 1986);
        books[4] = new bookpack.Book("On the Road", "Kerouac", 1955);

        // Exibe os detalhes de cada livro
        for (int i = 0; i < books.length; i++) {
            books[i].show();
        }
    }
}

Explanation of Changes:

Book class qualification:
The Book class is preceded by the package name (bookpack.Book) to tell the compiler where to find the class definition.
Without this qualification, the UseBook class would not be able to find Book in the bookpack package.

Required changes to the Book class:
Public class: The public class Book declaration allows Book to be accessible outside of its original package.
Public constructor: makes it possible to create Book class objects from another package.

Public show() method: so that book details can be displayed outside the bookpack package.

Directory Structure for Compilation and Execution:

src/
|-- bookpack/
|   |-- Book.java
|
|-- bookpackext/
|   |-- UseBook.java

Steps to Compile and Run:
Compile the Book class:

javac src/bookpack/Book.java

Compile the UseBook class from the src directory:

javac src/bookpackext/UseBook.java

Run UseBook:

java -cp src bookpackext.UseBook

Considerations:
When accessing a public class from another package, you can use the full qualified name or use the import statement to make the class easier to use without qualifying each instance.
The example shows how class member visibility (public, private, etc.) and per-package qualification are used to modularize code and control access between different parts of a Java program.

The above is the detailed content of Package Access Example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn