Package and access permission (2). access permission

Source: Internet
Author: User

Package and access permission (2). access permission

1. Access e in Java: Permission


1. public: public permission

Public String name;

It can modify both classes and member variables and member functions;

If the permission of a class is declared as public, the class name must be the same as the. java file name.

If the permission of a class is not public, you cannot access

To call a member variable or member function of an object outside a package, the member variable or member function must have the public permission.

Conclusion: There are no restrictions. classes in the same package or in different packages can be freely accessed.

2. private: private permission

Private String name;

Private can modify variables and functions. Once the variables and functions are modified to private, they can only be used in the current class, but cannot be used outside the class (regardless of the package)

3. default: Package-level access permission

String name; (if you do not write a permission modifier, the default permission is used)

Class, member variables, and member functions can be modified.

Free access to the same package

4. protected: protected permission


2. Import the Software Package

Import org. marsdroid. Person;

A relatively lazy method: import org. marsdroid. *; (indicates importing all the files under the org. marsdroid package)


Java classes without public modification, whether the access permission is package-level access permission or access permission under the same file

1. public: indicates that the data member and member function are open to all users and can be directly called by all users.

2. private: private indicates private. private means that no one except the class itself can directly use private property. private property cannot be infringed, even if it is a child or friend, none.

3. protected: for children and friends, protected is public and can be used freely without any restrictions. For other external classes, protected becomes private.

Scope: current class, same package, Child class, other package

Public √

Protected √ ×

Friendly √ ××

Private √ ×××

What is in java? What is the package access permission? C ++ Basics

Is there three classes a B C in first. java and three classes D E F in second. java? So first and second are two different packages? No, this is the way java files are organized, Because java requires that a public class must be in a file with the same name, but classes with multiple package access permissions can be in the same folder
First. java
Class A {void print () {System. out. println ("A") ;}} class B {public void print () {System. out. println ("B") ;}} class C {public void print () {System. out. println ("C") ;}} Second. java
Class E {public void print () {System. out. println ("E") ;}} class D {public void print () {System. out. println ("D") ;}} class F {public void print () {System. out. println ("F") ;}} a B C D E F are all in the same package, rather than ABC in the firstFirst package, and DEF in the Second package
Rewrite Second. java
Package package2; class E {public void print () {System. out. println ("E") ;}} class D {public void print () {System. out. println ("D") ;}} class F {public void print () {System. out. println ("F") ;}} you are now first. java cannot access the EDF class. This is the concept of the package. in java, ABC is in the default package and another package.) It cannot access the EDF class because they are the package access permission. The class has only two permissions: Package access permission and public permission.
Package access means that I can access some second methods in first. java or generate D E F objects?
If first. java and second. java is in the same package, so yes, but if it is not in the same package, and those classes or methods are packet access permissions, there is no way to access
Add a class Third. java
Package package2.third; class Third {public void print () {System. out. println ("E") ;}} class D {public void print () {System. out. println ("D") ;}} class F {public void print () {System. out. println ("F ");}}
All these classes are package access permissions. Can classes in E. java be accessed? No, the package does not have the concept of nesting, even if Third. java is in the sub-package of E. java, that is, E. java is in package2, and Third. java is in package2.third.
Java class access permissions (not internal classes, internal classes must be discussed separately) only public and package access permissions, or... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.