Detailed introduction to constructor in Java programming

This paper is mainly a summary for novices, people interested in Java language and those who have not systematically studied the basic knowledge of Java. In this paper, the constructor is described and discussed in detail, and my personal views on constructing functions in java object-oriented are also included. It is hoped that colleagues on the java learning road can have a clearer cognition and understanding. Of course, it's just a personal point of view. The level is limited. Please point out the shortcomings and exchange and learn from each other.

  1. Concept of constructor

Many Java novices get dizzy when talking about constructors. Let's take a look at what constructors are.

First, constructor is a special form of function. Where is the special? There is no need to define the return type in the constructor (void means no return value, please distinguish between the two), and the name of the constructor is exactly the same as the class name. The other features are the same as those of the function. It can have a parameter list, and there can be function overloading.

  2. Format of constructor

After understanding the basic concept of constructor, let's write a constructor. I hope you can understand and remember its format, and find the difference between it and ordinary functions through examples.

It should be noted here that if we do not declare a constructor in the class, the JVM will help us generate an empty parameter constructor by default; If we declare a constructor with a parameter list in the class, the JVM will not help us generate an empty parameter constructor by default. If we want to use an empty parameter constructor, we must explicitly declare an empty parameter constructor ourselves.

  3. Function of constructor

Through the introduction at the beginning, the outline of the constructor has gradually become clear, so why is there a constructor? What does a constructor do? Constructor is required by the idea of object-oriented programming. Its main functions are as follows:

1) create an object. When any object is created, it needs to be initialized before it can be used, so any class that wants to create an instance object must have a constructor.

2) object initialization. The constructor can initialize an object, and it initializes an object in accordance with its format (parameter list). It is a targeted initialization function.

  4. Difference between constructor and ordinary function

The following is a detailed analysis of the differences between constructors and ordinary functions. Through a comparison between the two, we hope to deepen our understanding of the concept of constructors.

1) different formats:

The constructor has no return type, and the function name is consistent with the class name of the class;

Ordinary functions have return types, and function names can be named according to requirements.

2) different call periods

The constructor runs when the object of the class is created;

Normal functions are executed only when the object is called.

3) different execution times

After an object is created, its constructor is executed only once, that is, when it is created;

After an object is created, its ordinary function can be executed multiple times, depending on the number of calls to the object.

Put it another way.

(1) . general functions are used to define the functions that an object should have. Constructors define the contents that an object should have when it is created before calling functions, that is, the initialization contents of the object.

(2) . the constructor is called by the JVM when the object is created to initialize the object. Generally, the function is executed when the object calls the function after the object is created.

(3) Ordinary functions can be called multiple times using the object, and the constructor is called when the object is created.

(4) . the function name of the constructor should be the same as the class name, while ordinary functions can only comply with the naming rules of identifiers.

(5) . constructor has no return value type.

  5. Usage scenario of constructor

After analyzing so much constructor information, when to use constructors? Since the constructor initializes new objects, when analyzing things in development and finding that things have some characteristics as soon as they appear, they can be defined in the constructor, which is convenient and fast, and also in line with the idea of object-oriented programming.

  6. Characteristics of constructor in inheritance

In inheritance, the object created by the subclass can call the public methods and properties of the parent class. Will the subclass call the constructor of the parent class? What is the relationship between the constructor of a subclass and the constructor of a parent class? What problems should we pay attention to in the constructor of subclasses? Let's answer these three questions.

1) will the subclass call the function of the parent class?

The subclass inherits from the parent class, and the constructor of the parent class will also be executed when the subclass object is initialized, because the subclass needs to use the properties in the parent class, and the subclass needs to know how to initialize, Therefore, subclass initialization will inevitably call the constructor of the parent class (unless the parent class has no properties, the description of this class is too poor, or there is no need to create this class).

2) what is the relationship between the constructor of the subclass and the constructor of the parent class?

The default first line in the constructor of the subclass has an implicit statement super (), which will access the null parameter constructor in the parent class. Unless there is no null parameter constructor in the parent class, the first line of the subclass constructor must explicitly call the constructor of the parent class, that is, super (int, x,...).

3) what problems should be paid attention to in the constructor of subclasses?

In the constructor of the subclass, the super () statement represents that the constructor of the parent class is called; The this () statement represents a call to the constructor of the subclass itself. It should be noted that if the two statements are explicitly written, they must be placed on the first line of the constructor, and the two statements cannot coexist. The first line in a constructor is either this () or super ().

Why on the first line? Because you need to initialize first.

Why not coexist? Because other constructors of this class represented by this () will also call super (), there is no need for super () to appear in this (), and repeated calls are meaningless. In other words, at least one constructor in a subclass starts with super (), which can of course exist implicitly; That is, at least one constructor does not start with this ().

  7. Extension of constructor

The above six have talked about the concept, characteristics, use and other issues of constructors. Let's introduce some extensibility knowledge related to constructors.

1) do all classes have constructors? Can constructors be privatized?

Since constructors are used to create and initialize objects, when a class does not need to create objects, it is not necessary to define constructors. However, in Java, all classes have constructors, but the constructors of some classes are hidden and invisible to developers, which is different from our expectations, However, from another perspective, it is also easy to understand. Because Java is object-oriented, the purpose of creating classes is to create objects or subclass objects. Therefore, classes without constructors (classes that cannot create objects) are meaningless.

For the second problem, a class can privatize its constructor when it does not want the outside world to create its object. This class provides methods to return the object, and in most cases, the provided object is unique. The singleton design pattern is a good example. When we need to ensure object uniqueness in development, This is often the case.

2) what are the similarities and differences between constructing code blocks and constructors?

The construction code block is represented by a pair of "{}". There are no specific requirements for the location of the code block, but it must be at the same level as the members of the class. In the parenthesis area, all objects of this class can be initialized, that is, the code block will be executed when the class object is created, and it takes precedence over the constructor. Constructors, as mentioned earlier, are targeted, and construction code blocks act on all objects of this class.

Constructor details:

(1) . when there is no constructor defined in the class, the system will assign a constructor with an empty parameter to the class. This is the default constructor in the class. If there is a custom constructor in the class, there will be no default constructor.

Note: it can be verified by javap command.

(2) . multiple constructors can be defined in a class for different initialization. Multiple constructors exist in the class in the form of overload, because the names of constructors are the same.

summary

The above is all about the constructor in Java programming. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Detailed explanation of Java synchronization function code

Introduction to avoiding hidden traps of equals method in Java programming

How to create and run a java thread

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>