Constructor function in Java
The constructor’s name is as same as the name of the class and can overload many different constructors. In JAVA language, the constructor function is as same as that in C++ language, which is generally called the construction method in JAVA language.
Constructor function’s features
Both user-defined constructors and default constructor function are a special method, which has the following characteristics,Steel Pipe Suppliers
- Automatically execute when an object is created.
- The method name of the constructor must be as same as the class’s name.
- The constructor has no return value type but may be with return. However can not have return value. In this case, it just means the end, not the return, and generally does not use return. (simply saying There is no return value type and no return value.)
- The main function of the construction method is to complete the initialization of the object, which can pass the parameters when defining the object to the domain of the object.
- A class can define more than one constructor. If no constructor is defined when a class is defined, the compiler automatically inserts a default constructor without parameters, which does not execute any code.
- The construction method can be overloaded by the number, type and order of the parameter.
- The constructors cannot be explicitly called.
The uses of Constructor function
The constructors are mainly used to initialize the object attributes when creating objects. When creating the objects, the objects automatically call their constructors. Generally speaking, constructor functions have the uses as three aspects,
- Create an identifier for the created object;
- Open up memory space for object data members;
- Complete initialization of object data members.