Introduction and understanding of java access permissions
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
##public | protected | private | |
√ | √ | ##× | |
√ | × | ##× | Can it be inherited |
√ | ##√ | × | 3. Understanding of visibility |
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.
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!

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

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

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

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]

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version