Home > Java > javaTutorial > body text

Java language access modifiers

PHPz
Release: 2024-08-24 06:39:02
Original
986 people have browsed it

Modificadores de acesso da linguagem Java

Types of Modifiers:

  • public: Allows access to members of a class by any program code, including methods from other classes.
  • private: Restricts member access within the class itself, preventing methods from other classes from accessing them directly.
  • protected: Used in inheritance, will be covered in Chapter 8.
  • Default access: If no modifier is used, access is the same as public, except in cases involving packages.

Examples of Modifiers:
Examples of how to use public and private in member declarations:

  • public String errMsg;
  • private accountBalance bal;
  • private boolean isError(byte status) { // ... }

Practical Demonstration:
Access Control in a Class:

  • Private members, like alpha in MyClass, can only be accessed by methods of the class itself.
  • Access to private members by public methods through accessor methods (getAlpha() and setAlpha()).
  • Attempts to directly access private members of another class will result in a compilation error.
class MyClass {
    private int alpha; // acesso privado
    public int beta;   // acesso público
    int gamma;         // acesso padrão (equivalente a public neste contexto)

    // Métodos para acessar alpha
    void setAlpha(int a) {
        alpha = a;
    }

    int getAlpha() {
        return alpha;
    }
}

Copy after login

Usage Example: AccessDemo.java Class

Result:

  • Access to the private member alpha can only be done through the public methods setAlpha() and getAlpha().
  • Direct access to alpha outside the MyClass class is prohibited, resulting in a compilation error.

Example: FailSoftArray:

  • Implements a "fault-resistant" array, where the array is encapsulated as a private member, accessed only by public methods.
  • Encapsulation: Protects the array from out-of-bounds access, preventing runtime exceptions.
  • Private Members: a, errval, and indexOK() are private, protecting the integrity of the array.
  • Public Member: length is public, allowing you to query the size of the array, similar to the implementation of standard arrays in Java.
  • Public Methods: put() and get() are used to store and retrieve values ​​from the array, controlling access securely.

Conclusion:
Access control is critical to successful object-oriented programming, especially when dealing with inheritance and ensuring data integrity.

Result:
The "fault-resistant" array prevents runtime errors when trying to access out-of-bounds indexes.
Access to array elements is done safely through the public methods get() and put(), with limit checking.
The program displays silent failures and then handles failures explicitly, showing when indexes are out of bounds.
These examples illustrate how Java access modifiers (public, private, and default access) are applied to control access to members of a class and ensure data integrity and security.

The above is the detailed content of Java language access modifiers. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!