Java package and access permission (26th sets)

Source: Internet
Author: User

The following is a java4android video tutorial from marschen.

Main content.
1. Access Permissions in Java
2. Import the Software Package

Access permission in Java
1. Public Permissions
2. Private permission
3. Default package-level access permissions
4. Protected is protected.
Instance description.

Package Org. marsdroid; // The public class can be modified. The member variables and member functions // public have no restrictions. In the same package, or classes in different packages can be freely accessed. // Private can modify member variables and member functions. // Private can only be used in this class. // default (no permission modifier is written, is the default permission) can modify the class, member variables and member functions // default in the same package, can be freely accessed. // public class // If a class is declared as public, the class name must be the same as the source file name. is this Java syntax? // Effect of public (cross-package access) // if the class is not public, it cannot be accessed from the external package. (that is, two classes are not in the same package) // two classes are in different packages. One Class calls the other, the class must be public // two classes in different packages. If you want to access a member function of a class, member function, and member variable, then these member functions, the member variable also requires publicpublic class person {public string name; Public int age; // public void introduce () {system. out. println ("name") ;}// private variable, which can only be used in the current class, regardless of whether the two classes are in a file. // Private function, which can only be used in the current class, no matter whether these two classes are not in the same file/private class, I will talk about it later. // default permission. By default, nothing is written. // default if person and test are in the same package, test can call the variables and functions of the person class. // default if person and test are not in the same package, test cannot call the variables and functions of the person class. // default indicates the package access permission.

Package COM. marsdroid; import Org. marsdroid. person; // if multiple packages need to be imported, use the wildcard Number * // import ort. marsdroid. *; Class test {public static void main (string ARGs []) {// because person and test are not in a package, the full name of person must be written. // Org. marsdroid. person P = new Org. marsdroid. person (); person P = new person (); // import Org. marsdroid. person, you can directly write p. name = "zhangsan"; p. age = 10 ;}}

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.