javase personal garbage review notes 02 some variables and modifiers

Access control
Insert picture description here
final variables:

Final means "last and final". Once a variable is assigned, it cannot be reassigned. Instance variables modified by final must explicitly specify initial values.

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

final method

The final method in the parent class can be inherited by the subclass, but cannot be overridden by the subclass.

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

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

public class Test{
    
    
    public final void changeName(){
    
    
       // 方法体
    }
}

final class

The final class cannot be inherited, and no class can inherit any characteristics of the final class.

public final class Test {
    
    
   // 类体
}

Abstract class:

Abstract classes cannot be used to instantiate objects. The sole purpose of declaring abstract classes is to extend the class in the future.

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

Abstract classes can contain abstract methods and non-abstract methods.

Abstract method

An abstract method is a method without any implementation, and the concrete implementation of the method is provided by the subclass.

Abstract methods cannot be declared as final and static.

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

If a class contains several abstract methods, then the class must be declared as an abstract class. An abstract class may not contain abstract methods.

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

public abstract class SuperClass{
    
    
    abstract void m(); //抽象方法
}
 
class SubClass extends SuperClass{
    
    
     //实现抽象方法
      void m(){
    
    
          .........
      }
}


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

public synchronized void showDetails(){
    
    
.......
}

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 that defines the variable to preprocess the data type of the class and variable.
public transient int limit = 55; // will not be persistent
public int b; // will be persistent

The volatile modifier
volatile-modified member variable is forced to re-read the value of the member variable from the shared memory every time it is accessed by a thread. Moreover, when the member variable changes, it will force the thread to write the changed 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.

public class MyRunnable implements Runnable
{
    
    
    private volatile boolean active;
    public void run()
    {
    
    
        active = true;
        while (active) // 第一行
        {
    
    
            // 代码
        }
    }
    public void stop()
    {
    
    
        active = false; // 第二行
    }
}

Normally, the run() method is called in one thread (the thread started in Runnable), and the stop() method is called in another thread. If the active value of the buffer in the first line is used, the loop will not stop when the active value in the second line is false.

But in the above code we used volatile to modify active, so the loop will stop.

Guess you like

Origin blog.csdn.net/qq_45864370/article/details/108490167