Access refers to the visibility of member variables and methods in this class to other classes

Think about it. When we modify a very large project, if all the variables and methods are common, then any class in the back end can modify its variables and methods, and it is very likely that the changes will cause the project to fail, which will have an impact on the client code

It is important to introduce the concept of access rights

In Java, there are four types of access: public, protected, package access (default), and private. If the access modifier is omitted, the default access is package access. The four permissions from Maximum to minimum are public > protected > Package Access rights > Private

Class member variables and method access rights

Put a watch you may not remember after looking at it for a long time:

The modifier Inside the class With the package A subclass Any place
public Yes Yes Yes Yes
protected Yes Yes Yes
Package access Yes Yes
private Yes

“Package access:” Permission without any modifier is “package access”, meaning that all classes of the current package can access this member, as shown in the table. For classes outside the package, this member becomes “private” and cannot be accessed

“Public:” Members decorated with public are available to any class, and can be accessed by any class. “public” members can be accessed by manipulating objects of that class

“Protected:” is accessible within the same class, within the same package, and within subclasses of other packages. To understand “protected” permissions, you need to understand “inheritance,” which deals with the concept of inheritance. Subclasses from inheritance can access “public, protected,”

“Private:” Makes this member inaccessible to all classes except the one that contains it, effectively sealing it off and preventing other classes from changing or deleting the method

Class access rights

Access permissions in Java can also be used to modify classes. Note, however, that for ordinary classes, the only access to classes is “public” and “package access.”

· If a class is declared with “public”, the class name must match the filename, including case and case. If it does not match, there will be a compilation error

· Each compilation unit (file) can have only one public class

· Of course, it is also possible to name the file without public

conclusion

It is also important to have access control over members to prevent others from touching parts that should not be touched, and to make it clear what is important and what can be ignored, simplifying the understanding of classes


If you need “Html homework, C language homework, Java course design and other learning materials”, please pay attention to the public account “Aime bacteria”, and reply “welfare” in the background to get it


Attention can improve learning efficiency! I’m Aime bacteria. See you next time! Peace~