Detailed control code for Java programming access permissions, detailed explanation of java permission code

Source: Internet
Author: User

Detailed control code for Java programming access permissions, detailed explanation of java permission code

This article focuses on the control of Java programming access permissions.

Modifier that you didn't pay attention to before. Generally, it is not added before the variable. One is not helpful, and the other is lazy, the difference between private and public projects will be large in the future.

(1) first, the package name

When using a class, such as a collection class, you need to introduce this package and then use the class under this package. For example:

package com.myown.iaiti;  public class Print {   static void print(String s){     System.out.println(s);   } } 

For custom packages, You can import your own packages and then print strings using your own writing method. I have tried to set the path of my package to CLASSPATH before, so you can compile the package in other directories without putting the package into this directory, otherwise, the JAR package cannot be found.

(2) public

Public and public. In the past, teachers often used Lao Tzu as a metaphor. This is a public thing. Lao Tzu gave it to you. public follows data members, indicating that it is available to everyone.

(3) private

Private, private, Lao Tzu's private property. Do not touch it. Except for Classes containing this data member in the package, other classes cannot be used, this part is not intended for external users.

Public class Print {public static void main (String [] args) {GetIt g = new GetIt ();}} class GetIt {// note that a file can only have one public class. If you want to add public class to the front of //, do not write it in the same file as Print private GetIt (){}}

The constructor GetIt () is not visible, which is invisible and useful. The Singleton mode is also used to control object creation.

Application of Singleton mode:

class A{   private A(){}    private static A a = new A();    public static A getInstace(){     return a;   } } 

Other classes cannot use new to create objects. Because the constructor is modified by private, it is useful when class A is extremely complex and consumes memory, I need to strictly control the creation of object. Now that we have provided a Singleton, we will introduce the Singleton. I have read some of them before. They are really classic and amazing.

The above is a hungry Chinese style, that is, it will help you new out, and will be used directly in the future, there is no thread problem, the disadvantage is that if it is useless, it is a waste of resources.

Lazy

public class A {        private A(){ }         private static A a;      public static A getInstance(){        if(a == null){          return a = new A();        }else{          return a;        }      }    } 

When it is used, it will help you with new. If it is null, it is new, but there is a thread problem. When synchronized is added, the efficiency is reduced. If multiple threads are used, one of them will be occupied.

The perfect method is to combine the two:

Public class A {// private static internal class. This class will be loaded only when there is A reference. private static class LazyA {public static A = new ();} public static A getInstance () {return LazyA. A ;}}

I can't understand this so-called static internal class before. The function is to achieve delayed loading, because only new classes are used and the getInstace method is used. There is no multithreading problem because, static classes belong to all external class objects and are loaded only once. After static internal classes are instantiated, they are class-level attributes. If they do not belong to a specific object, they are loaded only once, in this way, there will be no waste of resources or the efficiency of Multithreading is low. It's really amazing to come up with this method.

(4) protected

Print is a base class. Chinese people like to talk about the parent class, but foreigners think that sub-classes are more awesome. The base class is a foundation or foundation. protected is the property that Lao Tzu dedicated to his son.

Public class Print {protected void print () {} private void cannotprint () {}} public class PrintSon extends Print {void get () {print (); // cannotprint (); private method, subclass still cannot be used} public class NotSon {void get () {print ();}}

If protected is modified, the subclass can be obtained, which is between public and private.

(5) Class Access Permissions

Each file can have only one public class.

The class name and file name are consistent.

For a Singleton, it seems a little ahead of time if there is no foundation, but it will be easy to understand after the knowledge points are perfected. The control of access permissions is whether you want to directly use your own code.

Summary

The above is all the details about the Java programming access control code, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.