This article mainly studies related content on the control of Java programming access rights, as follows.
For modifiers that I didn't pay attention to before, generally, variables were not added before, one was because I didn't know what it was for, and the other was because I was lazy. When I encountered a project later, I found that there was still a big difference between private and public.
(1) First of all, the package name
When using a class, such as a collection class, you need to introduce this package and then use the class below the package. like:
package com.myown.iaiti; public class Print { static void print(String s){ System.out.println(s); } }Customized packages, by introducing your own packages, you can use the method you wrote to print strings in the future. I have tried to set the path of my own package into CLASSPATH before, so when compiling in other directories, you do not need to put the package into this directory, and it can also run normally, otherwise the JAR package will not be found.
(2) public
public, public, in the past, teachers often used Lao Tzu as a metaphor. This is a public thing, I will use it for you. Public follows the data members closely, which means that it is available to everyone.
(3) private
private, private, my private property, don't touch it. Except for the class containing this data member in the package that can be used, other classes cannot be used. This part is something that the designer itself does not want to show or modify for outsiders.
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 in front of //, then don't write it in the same file as Print private GetIt(){} }The constructor GetIt() is not visible, it is not visible, it can be seen that it is useful. The singleton pattern is also used in this way, controlling the creation of objects.
Application of singleton mode:
class A{ private A(){} private static A a = new A(); public static A getInstace(){ return a; } }Other classes cannot create objects through new, because the constructor is modified by private. The purpose is that when this class A is extremely complex and consumes memory, I need to strictly control the creation of A objects. Since we have given a single case, we will talk about the single case by the way. I have read some people’s writings before, it’s so classic and amazing.
The above is a hungry man style, which is to help you get new as soon as you come, and use it directly when you get new in the future. There is no threading problem. The disadvantage is that if it is useless, it will be a waste of resources.
Lazy style
public class A { private A(){ } private static A a; public static A getInstance(){ if(a == null){ return a = new A(); }else{ return a; } } }It will help you new when you use it, and it will be new when you use it, but there is a threading problem. If you add synchronized, the efficiency will be reduced because if you use it, one of them will be occupied.
The perfect way is to combine both:
public class A { // Private static inner class, the class will be loaded only when there is a reference private static class LazyA { public static AA = new A(); } public static A getInstance() { return LazyA.A; } }I can't understand this so-called static inner class before. The function is to achieve delayed loading, because it will only be used when new. Use the getInstance method. There is no multi-threading problem because the static class belongs to all external class objects and will only be loaded once. After the static inner class is instantiated, it is a class-level attribute, which does not belong to a certain object, but will only be loaded once. In this way, there will be no resource waste and there is no problem of inefficiency of multi-threading. The person who came up with this method is really amazing.
(4) protected
Print is the base category. Chinese people like to talk about parent category, but foreigners think that subcategories are more awesome. The base category is a foundation or foundation. protected is the property that I left specifically to my son.
public class Print { protected void print(){} private void cannot print(){} } public class PrintSon extends Print{ void get(){ print(); //cannotprint(); private private method, subclasses still cannot be used} } public class NotSon { void get() { print(); } }The protected modified subclass can be obtained, between public and private.
(5) Access permissions for Class
Each file can only have one public class.
The class name and file name are the same.
If you don’t have a basic foundation, it will be a bit ahead of time, but after the knowledge points are improved, it will be easy to understand. The control of access rights still depends on whether you want to use your part of your code directly for others.
Summarize
The above is all the detailed explanation of the control code for Java programming access permissions in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!