In Java, everything has some form of access control.
The control levels of access permissions are from the largest to the minimum: public, protected, package access permissions (no keywords) and private.
When used, the Java access modifiers, public, protected and private, are placed before each member (domain or method) definition in the class.
1. Access rights of class members
The only way to obtain access to a member is:
1). Make this member public. No matter who is, the member can be accessed;
2). Grant packet access to members by unaddressing modifiers and placing other classes in the same package, and other classes in the package can access the member;
3). Inherited classes can access both public members and protected members.
4). Provide accessor and mutator methods to read and change numeric values.
1. Package access permissions
The default access permission does not have any keywords, but pass refers to package access permissions, which means that all other classes in the current report have access to that member, but for all classes outside of this package, this member is indeed private.
Package access combines all relevant classes within the package so that they can easily interact with each other.
Note: If the two classes are in the same directory and do not set any package name for themselves, Java will automatically regard such files as the default package affiliated to the directory, so these files have package access to each other.
The following example illustrates this problem:
//Class Cake and Pie are in the same directory and are not explicitly displayed in any package class Pie{ void f(){ System.out.println("Pie.f()"); }}class Cake{ public static void main(String[] args){ Pie x = new Pie(); xf(); }}//The output is Pie.f()2.public: interface access permissions
Using the keyword public means that subsequent member declarations are available to everyone, especially client programmers who use class libraries.
3.private: You cannot access it
The keyword private indicates that no other class than the class containing the member cannot access the member. Other classes in the same package cannot access the private members of this class, so this is equivalent to isolating yourself.
This function of the private keyword has many uses, such as controlling how objects are created and preventing others from directly accessing a specific constructor (or all constructors). look
The following example:
class Sundae{ private Sundae(){} static Sundae makeASundae(){ return new Sundae(); }}public class IceCream { public static void main(String[] args){ Sundae x = Sundae.makeASundae(); }}In this example, we can create Sundae objects by calling makeASundae() method, but we cannot create them through the constructor.
This also applies to private fields in the class.
However, one thing to note is that you cannot think that other objects cannot have public references to the object just because the reference to an object in the class is private.
4.protected: Inherit access permissions
If a new package is created and inherits the class from another package, the only member that can be accessed is the public member of the source package.
Sometimes, the creator of a base class wants to assign access to a particular member to the derived class rather than all classes, which requires the keyword protected.
Note that protected also provides package access, that is, other classes within the same package can also access protected elements of this class.
2. Interface and implementation
Control of access rights is often referred to as hidden in concrete implementations.
Packaging data and methods into classes, and hiding specific implementations are often collectively called encapsulation.
For two important reasons, access permission control lies the boundaries of permissions inside the data type:
1. Set the limits that client programmers can use and cannot use. You can establish your own internal mechanism in the structure, without worrying that client programmers will accidentally treat the internal mechanism as part of the interface they use.
2. Separate the interface and specific implementation.
3. Class access permissions
In Java, access modifiers can also be used to determine which classes in the library are available to the user of the library.
Modifiers must be placed before the keyword class. For example:
public class Widget{......} or
improve access.Widget;
You should know that a class cannot be private (if the class is private, then no other class can access it except the class), nor can it be protected (in fact, an internal class can be private or protected, but this is a special case, as described in subsequent articles), and can only be package access permissions or public.
If you do not want others to access the class, you can specify all constructors of the class as private, preventing anyone from creating objects of the class. But there are exceptions to this, which cannot prevent you from creating the class inside the static member of the class. Let's look at the following example:
class Soup1{ private Soup1(){} public static Soup1 makeSoup(){ //Create an object using a static method return new Soup1(); }}class Soup2{ private Soup2(){} private static Soup2 ps1 = new Soup2(); //Create an object using singleton mode public static Soup2 access(){ return ps1; } public void f(){}}public class Lunch { void testPrivate(){ //Soup1 soup = new Soup1; Cannot be executed} void testSingleton(){ Soup2.access().f(); }}We can see that the constructors of Soup1 and Soup2 are both private, and no one can directly use the constructor to create objects of this class. But we can also use these two classes: create a static method in Soup1, use the constructor to create a Soup1 object and return its reference; the creation of Soup2 uses the singleton pattern in the design pattern, and can only create one of its objects. The object of the Soup2 class is created as a static private member of Soup2, so there is only one, and it cannot be accessed unless it is accessed through the public method.
In addition, some limitations are worth noting:
1. Each compilation unit can only have one public class.
2. The name of the public class must match exactly the file name that contains the compilation unit, including upper and lower case.
3. If there is no public class in the compilation unit, you can name the file at will.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.