Java access modifier (access controller)
Java controls access rights and other functions of classes, properties, and methods through modifiers, usually placed at the front end of a statement. For example:
public class class className { // body of class}private boolean myFlag;static final double weeks = 9.5;protected static final int BOXWIDTH = 42;public s tatic void main(String[] arguments) { // body of method}There are many modifiers in Java, divided into access modifiers and non-access modifiers. This section only introduces access modifiers, and non-access modifiers will be introduced later.
Access modifiers, also known as access control characters, refer to keywords that can control the usage rights of classes, member variables, and methods.
Access controllers are an important concept in object-oriented programming and can be used to protect access to classes, variables, methods, and constructors.
Java supports four different access permissions:
public: public
Classes, methods, constructors and interfaces declared as public can be accessed by any other class.
If several public classes that are accessed by each other are distributed in unused packages, you need to import the package where the corresponding public class is located. Due to the inheritance of the class, all public methods and variables of the class can be inherited by its subclasses.
The following method uses public access control:
public static void main(String[] arguments) { // body of method} The main() method of a Java program must be set to public, otherwise, the Java interpreter will not be able to run the class.
protected: protected
Variables, methods and constructors declared as protected can be accessed by any other class in the same package, or by subclasses in different packages.
The protected access modifier cannot modify classes and interfaces. Methods and member variables can be declared as protected, but the member variables and member methods of the interface cannot be declared as protected.
Subclasses can access methods and variables declared by protected modifiers, so that they can protect unrelated classes from using these methods and variables.
The following parent class uses the protected access modifier, and the subclass overloads the bark() method of the parent class.
public class Dog{ protected void bark() { System.out.println("Wow, don't come over"); }}class Teddy extends Dog{ // Teddy void bark() { System.out.println( "Wow, I Very scared, don't follow me"); }} If the bark() method is declared as private, then classes other than Dog will not be able to access the method. If bark() is declared public, then all classes can access the method. If we want the method to be visible only to a subclass of the class it is located in, declare the method as protected.
private: private
Private access modifiers are the strictest access level, so methods, variables and constructors declared as private can only be accessed by the class to which they belong, and classes and interfaces cannot be declared as private.
Variables declared as private access type can only be accessed by external classes through the public Getter/Setter methods in the class.
The use of private access modifier is mainly used to hide the implementation details of the class and protect the class's data.
The following class uses a private access modifier:
public class Dog{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge (int age) { this.age = age; }}In the example, the name and age variables in the Dog class are private variables, so other classes cannot directly obtain and set the value of the variable. To enable other classes to manipulate the variable, two pairs of public methods, getName()/setName() and getAge()/setAge() are defined, which are used to obtain and set the values of private variables.
This is a keyword in Java. This chapter will talk about it. You can click on the Java keyword to explain and preview it in detail.
Defining methods for accessing private variables in a class is customary to name it like this: prefix "get" or "set" in the variable name, and capitalize the initial letter of the variable. For example, the method to get the private variable name is getName(), and the method to set name is setName(). These methods are often used and have specific names, called the Getter and Setter methods.
Default: No keywords are used
No modifier declared properties and methods are visible to classes within the same package. All variables in the interface are implicitly declared as public static final, while the methods in the interface are public by default.
As shown in the following example, the definition of classes, variables, and methods does not use any modifiers:
class Dog{ String name; int age; void bark(){ // Barking System.out.println("Wow, don't come over"); } void hungry(){ // Hungry System.out.println("Master, I'm hungry"); }}Access control and inheritance
Please note the following rules for inheritance (readers who do not understand the concept of inheritance can skip here, or click on Java inheritance and polymorphic preview):
Methods declared as public in the parent class must also be public in the child class.
Methods declared as protected in the parent class are either declared as protected or public in the child class. Cannot be declared as private.
Methods declared by default modifiers in parent class can be declared as private in subclasses.
Methods declared as private in the parent class cannot be inherited.
How to use access control characters
Access control characters allow us to easily control the permissions of the code:
When you need to make the class you write access to all other classes, you can declare the access control of the class as public.
When you need to make your class accessible only by the classes in your own package, the access control character can be omitted.
When it is necessary to control the member data in a class, the member data access control characters in this class can be set to public, protected, or omitted.
Scope of Java variables <br />In Java, the scope of variables is divided into four levels: class level, object instance level, method level, and block level.
Class-level variables are also called global variables or static variables. They need to be modified with static keywords. You can compare and learn with static variables in C/C++. Class-level variables already exist after class definition, occupy memory space, can be accessed through class names, and do not need to be instantiated.
An object instance-level variable is a member variable. Only after instantiation will memory space be allocated to access.
Method-level variables are variables defined within methods, which are local variables.
Block-level variables are variables defined within a block. The survival period of the variable is this block. When this block appears, it disappears, such as the block of if and for statements. Blocks refer to code surrounded by braces, for example:
{ int age = 3; String name = "www.weixueyuan.net"; // Correct, the age and name variables System.out.println( name + "already" + age + "year-old"); }// Error, the age and name variables System.out.println( name + "already" + age + "years old"); illustrate:
In addition to accessing method-level variables, methods can also access class-level and instance-level variables.
Class-level and instance-level variables can be accessed inside a block. If the block is contained inside a method, it can also access method-level variables.
Method and block level variables must be displayed initialized otherwise they cannot be accessed.
Demo code:
public class Demo{public static String name = "Weixueyuan"; // Class-level variable public int i; // Object instance-level variable// Attribute block, run {int j = 2;// Block level Variable}public void test1() {int j = 3; // method-level variable if(j == 3) {int k = 5; // block-level variable}// Block-level variable cannot be accessed here, block-level variables are only Can access System.out.println("name=" + name + ", i=" + i + ", j=" + j);}public static void main(String[] args) {// No Create an object, access the class-level variable System.out.println(Demo.name);// Create an object and access it method Demo t = new Demo();t.test1();}} Running results:
Micro Academy name=Micro Academy, i=0, j=3