Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the top-level class modification in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand the top-level class modification problem in Java". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the top-level class modification problem in Java".

Java top-level class modification problem

I haven't thought about this problem carefully.

Tidy it up today

For top-level classes (external classes), there are only two modifiers: public and default. Because the last unit of the external class is a package, the external class has only two scopes: the same package, anywhere. Therefore, only two types of control permissions are required: package control permissions and public access permissions, which correspond to two control modifiers: public and default.

If the class uses the private modifier, it is an inner class. The upper level of the inner class is the outer class, so there are four kinds of access control modifiers: this class (private), the same package (default), the parent subclass (protected), and any location (public). When an inner class is decorated with private, it can only be used inside the external class of the class.

All of the above are common in daily use.

But why is this the case?

As you can imagine, it is impossible for a java project to be completed in a class. In the mvc pattern, the class is divided into three layers, calling the class layer by layer. Cannot be called if it is defined as private and protected.

In other words, for a java file, either run it alone or be called by other programs as a library, if the class of a java file is modified by private, then it is not available to other programs or classes, then it is useless as a separate file.

If it runs as a single file, how can class loading find it, because it is not visible to the outside world. At the same time, it also lost the meaning of the existence of the class. Therefore, the class has only public and default modifiers.

Private cannot be used to modify top-level class reasons

For top-level classes (external classes), there are only two modifiers: public and default.

Because the last unit of the external class is a package, the external class has only two scopes: the same package, anywhere.

Therefore, only two kinds of control permissions are required: package control rights and public access rights.

This corresponds to two control modifiers: public and default.

But private can modify class, but this class does not make the top-level class

If the inner class is the outer class, there are four kinds of access control modifiers: private, default, protected, public.

When an inner class is decorated with private, it can only be used inside the external class of the class.

Thank you for your reading, the above is the content of "how to understand the top-level class modification problem in Java". After the study of this article, I believe you have a deeper understanding of how to understand the top-level class modification problem in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report