Introduction to the

Modifiers are used to qualify types and type member declarations. The modifiers can be classified into class modifiers, method modifiers, and variable modifiers. It can be functionally divided into access control modifiers and non-access modifiers. Access modifiers control access permissions, and different access modifiers have different permissions, while non-access modifiers provide specific functionality.

Let’s take a look at modifiers from a functional point of view

Access modifier

Access modifier has four kinds: public, private, protected, default. Note that our default is not the same thing as the default in the non-access modifier!

By default, we mean the default, write nothing, be visible in the same package, and use no modifiers. Use objects: classes, interfaces, variables, methods.

Private only modifies methods and variables, not classes and interfaces. (After all, you can only access a class, so who can see it if you modify a class?) , modification method, commonly used in our when refactor the code to extract the common code for internal realization method of modified variable situation than we often see, because the Java wrapper feature, we define a class, often the class attributes defined as private, through getorset method to access these variables.

We often declare classes as public. Classes declared as public, interfaces, variables, and methods can be accessed by any class. It should be noted that a Java file can only contain one public class, and the main method must be modified with public. Otherwise, it cannot be recognized by the Java interpreter.

Protected is something we don’t use much in everyday development, except for variables, methods, and inner classes. Its main purpose is to protect subclasses. It means that subclasses can use it to modify members, but not others, and it’s sort of an inherited thing that’s passed to subclasses.

  • The base classprotectedMembers are visible within the package, andVisible to subclasses;
  • If a subclass is not in the same package as the base class, then in a subclass, the subclass instanceYou can visitFrom theInherited from the base classtheprotectedMethod, andCannot access the base class instancetheprotectedMethods.

Siphiwe sibeko may be the one shown above, just as we can, drawing badly 🙂

Non-access modifier

default

In JDK 8, the default method is implemented by default. Before JDK 8, the default method is implemented by default.

public interface Test {
  default void hello(a){
    System.out.println("Hello"); }}Copy the code

It’s like soy sauce

static

The static keyword is used to accessmethods or variables that do not depend on the object. When the class is loaded, the class name is used to accessmethods or variables that are static. The static keyword makes it easy to call (method/variable) without creating an object. Static modifier methods and variables are classes that do not depend on objects, so they cannot be accessed because they must depend on objects. But non-static methods can access static methods or variables, because when you create an object, the class must already be loaded, so it can be accessed.

One thing to note here is that static cannot be decorated with local variables

final

The final keyword is used with static to define constants in our system:

public static final String AUTHOR = "viyoung"
Copy the code

In addition to modifying variables, you can also modify classes and methods. Classes modified by final cannot be inherited, and methods modified by final can be inherited but cannot be modified.

abstract

Abstract applies to classes and methods. When applied to a class, it means that the class is abstract and needs to inherit extensions and cannot instantiate an object directly. When applied to a method, it means that the method needs to be extended. And cannot be final or static (a method that needs to be inherited and has no implementation why use these two methods, is not a self-deprecating 😂) :

abstract void test(a);
Copy the code

Abstract classes and abstract methods have the following relations: ** Those that include abstract methods are necessarily abstract classes, but those that do not include abstract methods are not necessarily non-abstract classes. * * 🤔

synchronized

Synchronized is familiar to those familiar with concurrent programming. It can be used on ordinary methods, static methods, and code blocks to lock and place multiple threads accessing a method/class at the same time. This is a brief introduction and will be covered in more detail later in concurrent programming.

volatile

Member variables that are volatile have visibility. Visibility refers to the visibility between threads. Changes made by one thread are visible to another thread. That’s the result of a thread modification. Another thread will see that in a second. For example, variables that are volatile are visible. Volatile variables do not allow in-thread caching and reordering, that is, direct memory modification.

This is just a brief introduction and will be covered in more detail later in concurrent programming.

transient

Member variables marked with the TRANSIENT keyword do not participate in the serialization process. Java’s serialization provides a mechanism for persisting object instances. When an object is persisted, there may be a special object data member that we do not want to use serialization mechanisms to store. To turn off serialization on a field for a particular object, you can prefix the field with the keyword TRANSIENT. When an object is serialized, transient variables are not included in the serialized representation, whereas non-transient variables are included.

private transient String name = "viyoung";
Copy the code

The public,