"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Java language access modifiers

Java language access modifiers

Published on 2024-09-01
Browse:159

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, such as 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;
    }
}

Usage Example: Class AccessDemo.java

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 the array size to be queried, 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 the 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.

Release Statement This article is reproduced at: https://dev.to/devsjavagirls/modificadores-de-acesso-da-linguagem-java-41?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3