Access control characters in Java

Source: Internet
Author: User
Tags control characters

First, take a picture:

For a class, the access control has only one public and no default modifier. Several other access modifiers are available for both variables and methods.

The following describes the specific use.

1. Public access Control (common)

Java classes are organized by the concept of a package, which is a loosely set of classes. Classes in the same package can easily access and reference each other without the need for any description two, but not for classes in different packages.

However, if a class is declared as public, it has the possibility of being accessed by classes in other packages, as long as the classes in those other packages introduce the public class in the program using the import statement, and the class can be accessed and referenced.

The main class for each Java program must be the public class and is based on the same reason.

Class variables that are decorated with public are called common variables. If the public variable belongs to a public class, it can be referenced by all other classes. The public modifier causes a decrease in the data encapsulation of security, so it generally reduces the use of the public domain.

2. Private access Control

A variable or method that is modified with private can only be accessed and modified by the class itself, and cannot be obtained and referenced by any other class, including subclasses of that class. The private modifier is used to declare the privately owned members of those classes, which provide the highest level of protection.

3. Securing the Access Control (protected)

Member variables modified with protected can be referenced by 3 kinds: The class itself, other classes in the same package as it, and subclasses of that class in other packages. The primary purpose of using the protected modifier is to allow subclasses of that class in other packages to access specific properties of the parent class.

4. Default Access Control

The default access control specifies that the class can only be accessed and referenced by classes in the same package, not by classes in other packages, which is also known as packet access.

Similarly, a variable or method within a class is provided with a packet access if it is not specified by the access control character. To put it simply, all classes defined in the same program belong to a package.

5. Summary

Briefly summarize the following from the large to the small range of their access:

Public: Accessible Anywhere

Protected: Same package and subclass visible

Default: Visible in same package

Private: Only this category is visible

6. Questions

6.1 Com.tuhooo and Com.tuhooo.test do you have anything to do with these two packages?

Visual inspection is not related, because the package is the function of the namespace, does not seem to have the concept of parent package and child package.

6.2 Actual use of protect this access control I use less, but I see a lot of the framework is used in the source of this protected.

6.3 The access control in Java is still very well understood, then how to implement the access control at the bytecode level?

7. The underlying exploration of the access control

Here we use a simpler class Student.java as an example, in which four types of access control characters are used.

 Package com.tuhooo.demo.test;  Public class Student {    privateint age ;     protected Double salary;      Public String name;     Char sex;}

The following two kinds of commands, first compile and then get the bottom byte code.

info. log

classfile/c:/users/tuhooo/ideaprojects/demo/src/com/tuhooo/demo/test/Student.class Last modified2018-5- -; Size302bytes MD5 checksum 92caa220b2c7efcdda2a68e7e53c63e7 Compiled from"Student.java"Public Class Com.tuhooo.demo.test.Student minor version:0Major Version: theflags:acc_public, Acc_superconstant pool: #1= MethodRef #3.# -         //java/lang/object. " <init> ":() V#2= Class # +            //com/tuhooo/demo/test/student#3= Class # -            //Java/lang/object#4=Utf8 Age #5=Utf8 I #6=Utf8 Salary #7=Utf8 D #8=Utf8 Name #9= Utf8 ljava/lang/String; #Ten=Utf8 Sex # One=Utf8 C # A= Utf8 <init>  # -=Utf8 () V # -=Utf8 Code # the=Utf8 linenumbertable # -=Utf8 SourceFile # -=Utf8 Student.java # -= Nameandtype # A:# -        //"<init>":() V# += Utf8 com/tuhooo/demo/test/Student # -= Utf8 java/lang/object{protectedDoublesalary;    Descriptor:d flags:acc_protected public java.lang.String name; Descriptor:ljava/lang/String; Flags:acc_publicCharsex;    Descriptor:c flags:public com.tuhooo.demo.test.Student (); Descriptor: () V flags:acc_public code:stack=1, locals=1, args_size=1         0: Aload_01: Invokespecial #1                  //Method java/lang/object. " <init> ":() V         4: Return linenumbertable:line3:0}sourcefile:"Student.java"

Major version number (major) The following two bytes represent the access flag (ACCESS_FLAGS), which is used to identify some class or interface-level access information, including: Whether this class is classes or interfaces, is defined as a public type, or is defined as an abstract type If the class is declared final, etc. The specific flags and the meanings of the markers are as follows:

Access_flags in a total of 16 flag bits can be used, currently only defines 8 of them, no use of the flag is required to be 0. As an example of the current student class, this is an ordinary class, not an interface, enumeration, or annotation, modified by the Public keyword but not declared as Fiana and abstract, and compiled with the compiler after JDK1.2, so its acc_public, acc_ The super flag should be true, while the other 8 flags in it are false, so its access_flags value should be: 0x0001|0x0020=0x0021.

The fields corresponding to the access control in 3, except private, are described in curly braces, which are the corresponding flags. So why use flags instead of a field with only one access control?

This is just taking advantage of the familiar access control to peek at a bit of bytecode, but it doesn't feel deep.

Access control characters in Java

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.