Thinking in Java Note 6 Access Control

Source: Internet
Author: User

  • Purpose
The consumer of the class library can use the changed and unchanged items separately.
  • Package: Library Unit
When writing a Java source code file (compilation unit: One. java files), each compilation unit has only one public class, and the name is the same as the file name. Other classes on the compilation unit are mainly public class services and cannot be seen outside the package.
  • Permission
The way to obtain access to a member: 1. Set the Member to public. Access is allowed everywhere. 2. Without the permission modifier, place other classes in the same package and access through package access. 3. The inherited subclass can access the public protected of the parent class. 4. The get set method is provided.
  • Private
    

public class Test {
private Test(){}

static Test makeATest(){
return new Test();
}
}


  
Private can be used to control how to create objects and prevent others from directly accessing a specific constructor. This example also has another effect, because the default constructor is a unique constructor and is private, it will hinder the inheritance of this class.

  • Protected
Contains the package access permission, and the inheritance class can also be accessed.
  • Interfaces and Implementations
Access control is called the hiding of specific implementations. Package data and methods into the class and hide the specific implementation. This is called encapsulation. The result is a data type with characteristics and behaviors of a colleague. Access Permissions define the boundaries of permissions within the data type: 1. Set the boundaries between the client programmer and the client programmer. 2. Separate the specific implementation of interfaces. If the structure is used in a group of programs, the client programmer can only send information to the interface, then it can change everything that is not public.
  • Class access permission
Class cannot be private protected (internal class can be ). The class can be the package access permission or public. If you do not want others to have access permissions to the class, you can set all constructors to private.
  • Singleton
Only one object of the class can be created.     

     public class Test {

private static Test test = new Test();
private Test(){}

static Test makeATest(){
return test ;
}
}


  • Static Import
Import static com... classname. *; indicates importing static methods in this class. Of course, you can only import a static method. You just need to replace. * with the static method name. Then, in this class, you can directly call static methods with the method name, instead of using the classname. method name method.

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.