---Access specifiers in the Java language

Source: Internet
Author: User

DAY03-----Access Specifiers in the Java language

First, an overview of the access specifier:

The access specifier is actually set up an access permission, only to set up this access right later to better encapsulate some of our variables or methods. So learning this specifier is very helpful for us to follow up on Java's encapsulation function.


Second, the access specifier:

1. What are the access specifiers?

In the Java language, an access specifier can implement a wrapper on a class member, with 4 specifiers, public, private, default, protected. Here's a one by one introduction.

1.1, Public: Open. can be accessed by any class, or by a class within a different package.

1.2, Private: Confidential. This member can only be accessed by the current class, and other classes are not allowed. Different packages can not be accessed

1.3, Protected: protected. The classes in the same package can be accessed, and only subclasses can access them in different packages, and inheritance can be accessed.

1.4. Default: Defaults. The classes inside the same package can be accessed.

2. The large order of access permissions:

Public--"protected"--"default" private

Public Private can modify a class, and the other 2 cannot be decorated.


3, an example to illustrate the 4 kinds of descriptors.


Create a Student class

Package www.com; public class Student {publicly string number;//: string username;//default: PRI vate int age;//Confidential: protected char gender; Protected: public void Display () {//can also be modified directly using the specifier. System.out.println ("Study No.:" +number); System.out.println ("Name:" +username); System.out.println ("Age:" +age);    System.out.println ("Gender:" +gender); }        }

Create a Userstudent class

Package www.dzx.one;  public class Userstudent {public static void main (string[] args) {//Create instance: Student stu = new    Student ();//Assignment Stu.number = "003";   Stu.username = "ZS";    Stu.age = 24;//This is not set because we previously set the private permission Stu.gender = ' M ';//Call Stu.display (); }}

Run the Userstudent class, you can see the assignment, and here you can obviously feel the access rights


Third, concluding remarks:

Come here. Our access modifiers are finished, so go ahead and use some of these specifiers! It's very important.

This article from "Lonely One Night" blog, reproduced please contact the author!

---Access specifiers in the Java language

Related Article

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.