search
HomeJavajavaTutorialIntroduction and understanding of java access permissions

Introduction and understanding of java access permissions

Jul 17, 2017 pm 05:59 PM
javaPermissionsaccess

1. What are access rights?

The visible scope of global variables and methods in the class, that is, the scope that can appear through object references.

2. Visible scope of permission modifier

##Package scope√√Other packagesCan it be inherited3. Understanding of visibility
##public protected private
##×
× ##×
##√ ×

One variable or method is different from another class Visibility of B means that the variable or method can be directly exposed to B through an object reference. On the contrary, invisible means that the variable or method cannot be accessed directly through an object reference in class B.

The fact that a variable or method is not visible to a class does not mean that the variable or method cannot appear in class B, but it cannot be directly exposed in class B, but can be exposed indirectly. Appears indirectly through method references of public or protected types.

package com.javase.temp;import org.junit.Test;public class TempTest {private String str = new String("abc");

    @Testpublic void printStr() {
        System.out.println(this.str);
    }
}

Obviously there is a member variable str in the TempTest class, which is of private type and is not visible to other classes, which means that the variable cannot be referenced through an object. Forms appear in other classes and can appear indirectly in other classes by calling the printStr method.

package com.javase.temp;import org.junit.Test;public class TempTest01 {

    @Testpublic void test01() {
        TempTest obj = new TempTest();
        obj.printStr();//通过TempTest类型的public类型的方法间接访问了private类型的变量,在访问时不知道private类型变量的情况    }
}

Output:

##Call the TempTest object obj in the TempTest01 class The method printStr in printStr outputs the value of the private variable str. str is not directly exposed in TempTest01, but is directly exposed in the method printStr of TempTest, and the method printStr is directly exposed in TempTest, thus making str indirectly exposed in TempTest.

Although the value of the private type variable in TempTest is output in TempTest01, this output is random and unconscious. TempTest01 has no way of knowing that the output result comes from the variable str, and the output is meaningless. .

A variable designed not to be directly accessed by the outside world is usually used as an intermediate process in this type of operation to assist other visible variables and methods.

The above is the detailed content of Introduction and understanding of java access permissions. 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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version