Home  >  Article  >  Java  >  How to use Java modifiers abstract, static and final

How to use Java modifiers abstract, static and final

WBOY
WBOYforward
2023-04-26 09:46:141045browse

Modifier abstract

1. Abstract can modify a class

(1) The class modified by abstract is called an abstract class
(2) Syntax:

# Abstract class Class name {}

(3) Features: Abstract classes cannot create objects separately, but you can declare the name of
abstract category name;
(4) Abstract classes can define member variables and member methods
(5) Abstract classes have construction methods. When used to create subclass objects, jvm creates a parent class object by default;
The abstract construction method is used in Applied when jvm creates a parent class object.

2. Abstract can modify methods

(1) The method modified by asbtract is called an abstract method
(2) Syntax:

Access modification Symbol abstract Return value type method name (formal parameter list);

Note: There is no order requirement for abstract and access modifiers
(3) Features: Abstract methods only have the declaration part, no method Implementation part (not even {}, ending with;)
(4) Note: Abstract methods can only be defined in abstract classes; but abstract methods can be defined in abstract classes as well as non-abstract methods

Subclass of abstract class:

(1) Syntax:

class subclass name extends abstract class name{}

( 2) Requirement: If the subclass does not want to become an abstract class, it must cover all abstract methods in the parent class of the abstract class (purpose: to complete the abstract method implementation part);
If the subclass does not cover all abstract methods in the parent class , must be defined as an abstract class, and objects cannot be created
(3) Application: Abstract class embodies polymorphic application

Abstract class name reference name = new subclass class name () ; // The reference of the parent type stores the object of the subtype

Modifier of static? (static)

1. Static can modify attributes

( 1) Properties modified by static are called static properties, static variables, and class variables
Note: Member variables are divided into: instance variables and static variables (or static properties, class variables)
(2) Position: Definition Within the class, outside the method, it is modified by static
(3) Syntax:

Access modifier static data type variable name;
Access modifier static data type variable name=value;

Note: There is no order requirement between access modifiers and static, but they must be in front of the data type
(4) Features: Static properties exist based on classes, and how many objects are created Irrelevant, shared by all objects
(5) Use:
a. Through the object name. Static property name
b. Directly through the class name. Static property name ——>Suggestion

Note: Instance variables must be accessed through object name.Instance variable name

2. Static can modify methods

(1) Methods modified by static are called static methods
(2) Syntax:

Access modifier static Return value type method name (formal parameter list) {
              // Method implementation, method body
  }

Note: There is no order requirement between access modifiers and static
(3) Use:
a. Directly pass the class name. Static method name (actual parameter); --》Recommendation
b. Through object name. Static method (actual parameters); -->Not recommended
(4) Static method syntax details:
a. Only static members of this class (static properties and static methods) can be accessed in static methods )
b. Non-static members of this class cannot be directly accessed in static methods (instance variables are non-static methods)
c. The this/super keyword cannot be used in static methods
d. Static methods can be inherited by subclasses
e. Static methods can only be overridden by static methods. Static methods do not reflect polymorphic applications.
(5) Static method application scenarios: Methods in tool classes are usually set to static methods for the convenience of users.

How to use Java modifiers abstract, static and final

3. Static can modify the initialization code block

(1) The initialization code block modified by static is called a static code block
(2) Static code Block position: defined within the class, the method is that {}

is modified by static class class name{
} static{
}
} }

(3) Function: When the class is loaded, the initialization of the static properties is completed in the order in which the static properties are defined
(4) Class loading:
a. Concept: jvm No. When using a class at a time, find the .class file corresponding to the class through classPath;
                                                                                                                                              and the . Methods, member methods, etc.);
Save the read information to the jvm memory, and only load one class per class.
             
                                                                                                           
                                                                                           
                                                             Create an object of this type at one time: complete the class loading first; then complete the creation of the object
   Call the sub -class static attribute or static method
② the first creation of the sub -class object: the first class loaded, and then complete the creation of the object
load: first complete the parent class loading Class load

Create Object: First complete the creation of the parent class, the creation of the recreation of the class object

modifiers? Modified variables

final can modify variables (local variables, member variables - instance variables and static variables)

(1) Features: variables modified by final are constants within the scope, Only one assignment is allowed and can be used multiple times

Note: Once the final modified variable is assigned, it cannot be modified

(2) Syntax:


Access modifier final data type variable Name = value;

(3) The final-modified instance no longer has a default value, and developers have the following opportunities to assign values ​​to it:

a. Initialize it when defining and assign a value

                                                                                                                                                                                                                                                                                                Since # # This.a=a;
public A(int a){

#(4) Final modified static variables no longer have a default value, developers The opportunities to assign a value to it are as follows:

a. Initialize it when defining and assign a value

b. Initialize it using a static code block

class A{
final static int n;

STATIC {
n = 5;
}
}

## (5) Final modified reference, which means that the storage object in the reference cannot be changed

2. Final can modify methods


Can be inherited by subclasses, but subclasses are not allowed to override.

3.Final can modify properties

Classes modified by final cannot Inherited, that is, there are no subclasses.

The above is the detailed content of How to use Java modifiers abstract, static and final. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete