Java Access Rights control

Source: Internet
Author: User
Tags modifiers

Why should Java have access control?

The settings for access permissions are related to the refactoring of the code. In a project, most of the time and money is invested in the maintenance of the code. Maintenance will certainly modify the existing unreasonable code. But in the process of refactoring, there is the problem: how do you ensure that the normal use of the client code that uses the code to be modified is not affected? How do I know what code has been used to modify the client code? All of this depends on the permission modifier words to achieve. The permission modifiers explain what the client code can use, as long as the code (which I don't exactly call the interface) does not change, the normal use of the client code is guaranteed no matter how the internal code changes.

Static import: Import static, only one class can be imported from a statically member function and member variable

Access permission modifiers for member functions or member variables:

    1. Public: Publicly accessible, which means that the repair can be accessed everywhere
    2. Package access: Also known as the default access permission, when a member function or variable is not modified by any modifier is the package access permissions, indicating that the current package can be accessed by all classes. Sometimes it's called friendly. Package access allows all related classes within the current package to be combined so that they can interact easily with each other. When a class does not show the package by its keyword, it is the default package.
    3. Private access, which means that except where the class can be accessed, all other places cannot be accessed. You can control how objects are created or control how many objects are created through the private keyword-decorated constructor
Class a{  Private A () {} public  static a Geta ()  {      return new A ();        }            }   

4. Protected: Inherited access, which means that it can be accessed by the current package, as well as the subclass of the class.

Interface and implementation:

The control of access rights is often referred to as the concealment of a specific implementation. Wrapping data and methods into classes, and the concealment of concrete implementations, is often referred to as "encapsulation." The result is a data type with characteristics and behavior at the same time.

For two very important reasons, the bounds of access control are drawn inside the data type. The first is to set the bounds that the client programmer can and cannot use. You can build your own internal mechanisms without worrying that the client programmer treats the internal mechanisms as part of the interface that they can use. The second reason is to detach the interface from the specific implementation. (To be honest, this part is not understood, especially the boundary is within the data type, which is very confusing.) )

The access rights of the class:

    1. Public access rights
    2. Package access rights, explained above

As mentioned earlier, if you do not specify an access modifier for class access, it defaults to the package access permission. This means that objects of this class can be created by any other class within the package, but outside of the package is not possible. (It is important to remember that all files in the same directory that do not have an explicit package declaration are considered to be part of the default packages in that directory.) However, if a static member of the class is public, the client programmer can still invoke the static member, although they cannot generate objects of that class

At first glance this passage, it is very puzzling, why a package access to a class, can have public static type members can be accessed in the outsourcing. So the Internet inquiry, after a senior's guidance, finally understand what meaning.

There is one more example before the passage:

: c05:lunch.java//demonstrates class access specifiers. Make a class//effectively private with private Constructors:class Soup {  private Soup () {}  //(1) Allow creation Via static method: public  static Soup Makesoup () {    return new Soup ();  }  (2) Create a static object and return a reference  //upon request. ( The "Singleton" pattern):  private static Soup PS1 = new Soup ();  public static Soup access () {    return PS1;  }  public void F () {}}class Sandwich {//Uses Lunch  void F () {new Lunch ();}} Only one public class allowed per File:public class Lunch {  void Test () {    //Can ' t do this! Private constructor:    //! Soup priv1 = new Soup ();    Soup priv2 = Soup.makesoup ();    Sandwich f1 = new Sandwich ();    Soup.access (). f ();  }}

What the public static refers to in this passage is

  public static Soup access () {    return PS1;  }

The static member can be called, although they cannot generate objects of that class, the class object cannot be generated because the constructor is private and access is the same package.

Specific problem address: http://segmentfault.com/q/1010000002549128

Thinking in Java note, if there is a wrong place, also look at ^_^

Java Access Rights control

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.