Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Java modifiers abstract, static and final

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, Xiaobian introduces in detail how to use the Java modifiers abstract, static and final. The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the Java modifiers abstract, static and final" can help you solve your doubts.

Modifier abstract (abstract) 1. Abstract can modify a class

(1) the class modified by abstract is called abstract class.

(2) grammar:

Abstract class class name {}

(3) Features: abstract classes cannot create objects alone, but can declare references

Abstract class name reference name

(4) Abstract classes can define member variables and member methods.

(5) Abstract classes have constructors. When creating subclass objects, jvm creates a parent class object by default.

Abstract constructors are applied when jvm creates a parent object.

2. Abstract can modify the method

(1) the method modified by asbtract is called abstract method.

(2) grammar:

The access modifier abstract returns the value type method name (formal parameter list)

Note: abstract and access modifiers have no sequential requirements

(3) Features: abstract methods have only declaration parts, but no method implementation parts (not even {}, ending with;)

(4) Note: abstract methods can only be defined in abstract classes; however, both abstract and non-abstract methods can be defined in abstract classes.

Subclasses of abstract classes:

(1) grammar:

Class subclass name extends abstract class name {}

(2) requirement: if a subclass does not want to be an abstract class, all abstract methods in the parent class of the abstract class must be overridden (purpose: to partially supplement the implementation of the abstract method)

If the subclass does not override all abstract methods in the parent class, it must be defined as an abstract class and objects cannot be created

(3) Application: abstract classes reflect the application of polymorphism.

Abstract class name reference name = new subclass name (); / / references to the parent type store objects of the subtype

Static of modifiers? (static) 1. Static can modify attributes

(1) the attributes modified by static are called static attributes, static variables and class variables.

Note: member variables are divided into instance variables and static variables (or static attributes, class variables).

(2) location: defined within the class, outside the method, modified by static

(3) grammar:

Access modifier static data type variable name

Access modifier static data type variable name = value

Note: there is no order requirement between the access modifier and static, but both must precede the data type

(4) Features: static attributes exist based on classes, regardless of how many objects are created, and are shared by all objects.

(5) use:

a. By object name. Static property name

b. Directly through the class name. Static property name-- > recommended

Note: the instance variable must pass through the object name. Instance variable name for access

2. Static can modify the method

(1) the method modified by static is called static method.

(2) grammar:

The access modifier static returns the 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 through the class name. Static method name (argument);-"recommendation

b. By object name. Static method (argument);-- > not recommended

(4) the syntax details of static methods:

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 accessed directly in static methods (instance variables + non-static methods)

c. Cannot this/super keyword in static method

d. Static methods can be inherited by subclasses

e. Static methods can only be overridden by static methods, and static methods do not reflect polymorphic applications.

(5) static method application scenario: usually set the method in the tool class to static method, in order to facilitate the user to call

3. Static can modify the initialization code block

(1) the initialization code block modified by static is called static code block.

(2) the location of the static code block: defined within the class, the method is {} modified by static

Class class name {

Static {

/ / static code block

}

}

(3) function: when the class is loaded, the initialization of the static property is completed in the order of the static attribute definition and the static attribute definition.

(4) Class loading:

a. Concept: the first time jvm uses a class, find the .class file corresponding to the class through classPath

And read the information of this class to the .class file

(package name, class name, parent class, attribute, constructor, member method, etc.)

Save the read information to jvm memory, and a class loads only once.

b. Class loading time: (what does jvm use a class for the first time)

i. The first one to access the static members of this class (static properties and static methods)

ii. Create this kind of object for the first time: finish loading the class first, and then complete the creation of the object

iii. Subclass loading, which first causes its parent class to load: first add to the parent class, and then load the subclass

① calls subclass static properties or static methods for the first time

② creates subclass objects for the first time: advanced row classes are loaded, and then the object is created.

Loading: first complete the class loading of the parent class, and then finish the class loading of the subclass

Create objects: first complete the creation of parent class objects, and then the creation of subclass objects

Final of modifiers? (final) 1. Final can modify variables

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

(1) Features: variables modified by final can be assigned only once and can be used more than one time.

Note: once the final modifier variable is assigned, it cannot be modified.

(2) grammar:

Access modifier final data type variable name = value

a. It is initialized and assigned when it is defined.

Final int a = 3

b. Using construction method to complete assignment

Class A {

Final int a

Public A (int a) {

This.a=a

}

}

(4) final-modified static variables no longer have default values, and developers can assign values to them as follows:

a. Initialize it at definition time, assign a value to it

b. Initialize it with a static code block

Class A {

Final static int n

Static {

N = 5

}

}

(5) A reference modified by final, which means that the object stored in the reference cannot be changed

2. Final can modify the method

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

Third, final can modify attributes.

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

After reading this, the article "how to use the Java modifiers abstract, static and final" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report