The Function and Scope of Various Modifiers in java

  • 2021-12-09 08:57:20
  • OfStack

Directory Access Modifier Scope Static Modifier Features Static Use Precautions Static Advantages and Disadvantages When a member variable is statically modified, the difference between a member variable and a non-static member variable

Access modifier

private 缺省 protected public

Scope of action

访问修饰符\作用范围 所在类 同1包内其他类 其他包内子类 其他包内非子类
private 可以访问 不可以 不可以 不可以
缺省 可以 可以 不可以 不可以
protected 可以 可以 可以 不可以
public 可以 可以 可以 可以
private

Properties and methods modified by private cannot be accessed by other classes, and subclasses cannot inherit or access them. It can only be accessed within the class in which it belongs.

缺省

Variables or methods without access modifiers can be accessed by the class in which they belong, and can be accessed or inherited by other classes in the same package. But it cannot be accessed by other packages.

protected

Methods and properties modified by protected can be accessed and inherited within the same package. In different packages, subclasses can be inherited, but non-subclasses cannot be accessed.

public

Methods and properties are preceded by public modification and can be accessed by classes in any package.

In addition, if a class wants to be imported by another package, it must be declared as public. Class decorated with public, the class name must be the same as the file name.

Characteristics of static modifiers

static is a modifier that modifies members (member variables and member functions)

1. Static members are loaded as the class is loaded.

2. Static members take precedence over objects.

3. Static members are shared by all objects

4. Static members have one more call mode, which can be called directly by class name.

Considerations for Static Use

1. Static methods can only access static members, while non-static methods can access both static and non-static.

2. this and super keywords cannot be defined in static methods. Because this stands for an object. When there is a static state, there may be no object. Therefore, when the static method runs, this is not represented by any object. Simply put, advanced memory data cannot access backward memory data, but backward memory data can access advanced memory data.

3. The main function is static

Advantages and disadvantages of static state

Advantages: There is one more way to call static members. Can be called directly by class name format: class name. Static member. It can also be called by an object.

Disadvantages: Static methods can only access static members, which leads to access limitations.

Statically decorated data objects share data stored in the static area of the method area.

Non-static data is unique to every 1 object. Is stored in the heap memory to which the object belongs.

When a member variable is statically decorated, the difference between it and a non-static member variable

1. Static variables are also called class variables, that is, variables that can be called directly by class names. This variable belongs to the class. Non-static variables become member variables, or instance variables, which are called by objects and belong to specific objects.

2. Static variables load as the class loads, which means they disappear as the class disappears. The longest life cycle. Instance variable, which loads as the object is created and disappears as the object disappears. Exist according to the life cycle of an object.

3. Static variables are stored in the static section of the method section. The instance variable exists in the heap memory to which the object belongs.

4. Static variable data, shared by all objects. Instance variables are unique data in objects


Related articles: