Explain Java modifiers

The Java language provides many modifiers, which are mainly divided into the following two categories:

Modifiers are used to define classes, methods, or variables, and are usually placed at the front of a statement. Let's illustrate with the following example:

Access modifier

In Java, access control characters can be used to protect access to classes, variables, methods, and constructor methods. Java supports four different access rights.

The default, also known as default, is visible in the same package without any modifiers.

Private, specified with the private modifier, visible within the same class.

Common, specified with the public modifier, visible to all classes.

Protected, specified with the protected modifier, is visible to classes and all subclasses in the same package.

Default access modifier - do not use any keywords

Variables and methods declared with default access modifiers are visible to classes within the same package. The variables in the interface are implicitly declared as public static final, while the access permission of the methods in the interface is public by default.

example:

As shown in the following example, variables and methods can be declared without any modifiers.

Private access modifier - Private

The private access modifier is the strictest access level, so the methods, variables and construction methods declared private can only be accessed by their own classes, and classes and interfaces cannot be declared private.

Variables declared as private access types can only be accessed by external classes through public getter methods in the class.

The private access modifier is mainly used to hide the implementation details of the class and protect the data of the class.

The following classes use the private access modifier:

In the instance, the format variable in the logger class is a private variable, so other classes cannot directly get and set the value of this variable. To enable other classes to manipulate this variable, two public methods are defined: getformat () (return the value of format) and setformat (string) (set the value of format)

Public access modifier - public

Classes, methods, constructors, and interfaces declared public can be accessed by any other class.

If several mutually accessible public classes are distributed in different packages, you need to import the package where the corresponding public class is located. Due to the inheritance of a class, all public methods and variables of a class can be inherited by its subclasses.

The following functions use public access control:

The main () method of the Java program must be set to public, otherwise, the Java interpreter will not be able to run this class.

Protected access modifier - protected

Variables, methods, and constructors declared protected can be accessed by any other class in the same package or by subclasses in different packages.

Protected access modifiers cannot modify classes and interfaces. Methods and member variables can be declared protected, but interface member variables and member methods cannot be declared protected.

Subclasses can access the methods and variables declared by the protected modifier, so that unrelated classes can be protected from using these methods and variables.

The following parent class uses the protected access modifier, and the child class overloads the openspeaker () method of the parent class.

If the openspeaker () method is declared private, classes other than audioplayer will not be able to access it. If openspeaker () is declared public, all classes can access this method. If we only want the method to be visible to subclasses of its class, declare the method protected.

Access control and inheritance

Note the rules inherited by the following methods:

Non access modifier

In order to implement some other functions, Java also provides many non access modifiers.

Static modifier to create class methods and class variables.

Final modifier is used to modify classes, methods and variables. Finally modified classes cannot be inherited, modified methods cannot be redefined by inherited classes, and modified variables are constants and cannot be modified.

Abstract modifier to create abstract classes and abstract methods.

Synchronized and volatile modifiers are mainly used for thread programming.

Static modifier

Static keyword is used to declare static variables independent of objects. No matter how many objects a class instantiates, its static variables have only one copy. Static variables are also called class variables. Local variables can be declared as static variables.

Static keyword is used to declare static methods independent of objects. Static methods cannot use non static variables of a class. The static method obtains the data from the parameter list, and then calculates the data.

For access to class variables and methods, you can directly use classname Variablename and classname Methodname.

As shown in the following example, the static modifier is used to create class methods and class variables.

The editing results of the above examples are as follows:

Started with 0 instances Created 500 instances

Final modifier

Final variable:

The final variable can be explicitly initialized and can only be initialized once. References to objects declared final cannot point to different objects. But the data in the final object can be changed. In other words, the reference of the final object cannot be changed, but the value inside can be changed.

The final modifier is usually used with the static modifier to create class constants.

example:

Final Method

The final method in a class can be inherited by a subclass, but cannot be modified by a subclass.

The main purpose of declaring the final method is to prevent the contents of the method from being modified.

As shown below, the method is declared using the final modifier.

Final class

Final classes cannot be inherited. No class can inherit any features of final classes.

example:

Abstract modifier

Abstract class:

Abstract classes cannot be used to instantiate objects. The only purpose of declaring abstract classes is to expand them in the future.

A class cannot be modified by abstract and final at the same time. If a class contains abstract methods, the class must be declared as an abstract class, otherwise a compilation error will occur.

Abstract classes can contain abstract and non abstract methods.

example:

Abstract method

Abstract method is a method without any implementation, and the specific implementation of the method is provided by subclasses. Abstract methods cannot be declared as final and strict.

Any subclass that inherits from an abstract class must implement all abstract methods of the parent class unless the subclass is also an abstract class.

If a class contains several abstract methods, the class must be declared as an abstract class. Abstract classes can contain no abstract methods.

The declaration of an abstract method ends with a semicolon, for example: public abstract sample();

example:

Synchronized modifier

The synchronized keyword declares a method that can only be accessed by one thread at a time. The synchronized modifier can be applied to four access modifiers.

example:

Transient modifier

When the serialized object contains an instance variable modified by transient, the Java virtual machine (JVM) skips the specific variable.

This modifier is included in the statement defining the variable and is used to preprocess the data types of the class and variable.

example:

Volatile modifier

Each time a member variable modified by volatile is accessed by a thread, it forces the value of the member variable to be re read from the shared memory. Moreover, when the member variable changes, the thread is forced to write the change value back to the shared memory. In this way, at any time, two different threads always see the same value of a member variable. A volatile object reference may be null.

example:

In general, the run () method is invoked in a thread and the stop () method is invoked in another thread. If the value of active in line 1 in the buffer is used, the loop will not stop when active in line 2 is set to false.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>