java constructor

1. Constructor:

It is used to initialize an object, it is to initialize the corresponding object, it is pertinent, a kind of function.

Features:

1: The name of the function is the same as the name of the class in which it is located.

2: No need to define the return value type.

3: The function has no specific return value.

In a class, there can be multiple constructors, because they all have the same function name, so they can only be distinguished by the parameter list. Therefore, if there are multiple constructors in a class. Their existence is manifested in repetition.

2. What is the difference between constructing code blocks and constructors?

Constructing code blocks: Initialize all objects, that is, all objects will call a code block. As soon as the object is created. This code block is called.

Constructor: Initializes the corresponding object. It's targeted.

 

3. Static code block:

It is a code block area marked with a static keyword. defined in the class.

Role: can complete the initialization of the class. Static code blocks are executed as classes are loaded, and only once (only once for new objects). If it is in the same class as the main function, it takes precedence over the main function.

 

Execution order when static code blocks, construction code blocks, and constructors exist at the same time:

Static code blocks (executed when the class is loaded).

Construct the code block (before the constructor).

Constructor (executed when the object is created);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325682681&siteId=291194637