2023.8-java-class modifier

Review the ts modifier

TS method modifier
public Do not write the default, the largest scale permissions
protected Modified methods can only be used in base classes and subclasses, instances cannot be accessed, and can be obtained with getter
private Modified methods, Variables or methods that can be modified with getters to obtain
static can only be accessed within the base class, accessed through the class name (self), called a class The method
abstract can only be used in the abstract class, the modified method cannot contain the method body, and the subclass inheritance must implement the abstract method, unless the base The class is also an abstract class

External class class modifier:

In Java, modifiers for outer classes are similar to modifiers for inner classes. The following are external class modifiers:

  1. public: External classes modified by public can be accessed from anywhere.

  2. abstract: The external class modified by abstract is an abstract class, which cannot be instantiated directly, but can only be used as the base class of other classes.

  3. final: The external class modified by final is the final class and cannot be inherited.

  4. Default (no modifier): A class without an explicitly specified modifier has the default access level, which means that it can only be accessed within the same package.

Here is sample code demonstrating the use of different modifiers:

// package name: com.example

public class PublicClass { // public class
    // class implementation
}

abstract class AbstractClass { // abstract class
    // Class implementation
}

final class FinalClass { // final class
    // class implementation
}

class DefaultClass { // default class
    // class implementation
}

In Java, a nested class is a class defined inside another class. Nested classes can be declared with a variety of different modifiers. Here is some sample code to demonstrate the use of different modifiers in nested classes:

public class OuterClass {
    
    private static int outerData;
    
    // nested class
    public static class NestedPublicClass {
        // nested public class
        private int nestedData;
        
        public void nestedMethod() {
            // access external class data
            outerData = 10;
            // execute nested class logic
        }
    }
    
    // nested abstract class
    protected abstract class NestedAbstractClass {
        public abstract void nestedMethod();
    }
    
    // nested private class
    private static class NestedPrivateClass {
        private int nestedData;
        
        private void nestedMethod() {
            // execute nested class logic
        }
    }
    
    // Nested final class
    final class NestedFinalClass {
        //Execute nested class logic
    }
}
// package modifier;

public class index {
    public static void main(String[] args) {
        System.out.println("111");
        test2 t2 = new test2();
        test1 t1 = new test1();
        t2.aa();
        test2.aa();

    }

   static protected class test1 {
        public test1() {
            System.out.println("test1");
        }

        public static void main(String[] args) {
            System.out.println("test111");
            test2.aa();
        }
    }

   static private class test2 {
        public test2() {
            System.out.println("test2");
        }

        public static void main(String[] args) {
            System.out.println("test2");
        }

       static private void aa() {
            System.out.println("aa5454");
        }
    }

    //t2.aa();

}

class test extends index {

    test() {
        // super()
    }
}

External classes can only be used only public, abstract & final are permitted

There are no restrictions on internal classes, and there is no order restriction between multiple modifiers. Internal classes can access each other by adding the static modifier.

Default access modifier – no keywords used:

If no access modifier is specified in the definition of a class, variable, method, or constructor, they default to a default access modifier.

The access level of the default access modifier is package-level, which means it can only be accessed by other classes in the same package.

Private access modifier -private:

The private access modifier is the most restrictive 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 strong>.

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

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

Public access modifier -public:

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

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

Protected access modifier-protected:

protected needs to be analyzed and explained from the following two points:

  • The subclass is in the same package as the base class: variables, methods and constructors declared as protected can be accessed by any other class in the same package;

  • The subclass and the base class are not in the same package: in the subclass, the subclass instance can access the protected method inherited from the base class, but cannot access the protected method of the base class instance.

protected can modify data members, constructors, and method members, cannot modify classes (except inner classes).

Interfaces and their member variables and member methods cannot be declared as protected.

Access control and inheritance

Note the following rules for method inheritance:

  • Methods declared public in the superclass must also be public in the subclass.

  • A method declared as protected in the parent class is either declared as protected or public in the subclass, and cannot be declared as private.

  • Methods declared as private in the parent class cannot be inherited by subclasses.

Summary: The subclass inherits from the parent class method (public protected) and the modifier must be greater than or equal to the access level of the method in the parent class

Non-access modifier:

The static modifier is used to modify class methods and class variables.

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

The abstract modifier is used to create abstract classes and abstract methods.

The synchronized, volatile, and transient modifiers are mainly used for thread programming.

static modifier

  • Static variable:

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

  • Static method:

    The static keyword is used to declare a static method independent of the object. Static methods cannot use non-static variables of the class. Static methods get data from the parameter list, and then calculate these data.

Access to class variables and methods can be accessed directly using classname.variablename and classname.methodname.

final modifier

final variable:

final means “the final, final” meaning, once the variable is assigned, it cannot be reassigned. Instance variables modified by final must explicitly specify the initial value.

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

final method

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

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

final class

A final class cannot be inherited, no class can inherit any characteristics of a final class.

abstract modifier

Abstract class:

Abstract classes cannot be used to instantiate objects, and the only purpose of declaring an abstract class is for future extensions to the class.

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

An abstract class can contain both abstract and non-abstract methods.

Abstract method

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

Abstract methods cannot be declared 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 abstract. An abstract class may not contain abstract methods.

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

synchronized modifier (synchronized; synchronized)

The method declared by the synchronized keyword 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 (short transient)

When a serialized object contains an instance variable modified by transient, the Java Virtual Machine (JVM) skips that particular variable.

This modifier is included in the statement that defines the variable and is used to preprocess the data type of the class and variable.

public transient int limit = 55; // Will not persist
public int b; // persistence

volatile modifier (volatile, volatile, capricious; (mood) volatile, irritable, sudden onset; (liquid or solid) volatile, gasified; (computer memory) volatile)

Each time a volatile-modified member variable is accessed by a thread, the value of the member variable is forced to be re-read from the shared memory. Moreover, when a member variable changes, the thread is forced 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) // first line
        {
            // code
        }
    }
    public void stop()
    {
        active = false; // second line
    }
}

Normally, the run() method is called on one thread (thread started by Runnable), and the stop() method is called on another thread. If the active value of the buffer in First Line is used, then the active value in Second Line is When false the loop will not stop.

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