Constructors. Default constructors. Calling class constructors from other constructors
Contents
- 1. What is a default constructor?
- 2. In some cases, the default constructor is generated in the class automatically? Example
- 3. Calling constructors from other constructors. Example
- 4. What constraints (requirements) are imposed on calling other constructors from the class constructor?
- 5. Can be constructor called from the usual class method?
- Related topics
Search other websites:
1. What is a default constructor?
The default constructor is a constructor without parameters. The default constructor can be declared explicitly in the class or generated automatically. The default constructor can be declared explicitly in the class or generated automatically.
In the most general case, for the ClassName class, the default constructor is as follows:
class ClassName { ... // declare the constructor ClassName() { // body of constructor // ... } ... }
2. In some cases, the default constructor is generated in the class automatically? Example
If the class does not declare any constructor, then the default constructor will be generated. That is, the default constructor is automatically generated in the class only if the class does not contain implementations of other constructors. If the class contains an implementation of at least one constructor with parameters, then to declare the default constructor, it must be explicitly specified in the class.
For example. In the next class declaration, the default constructor is generated automatically
class CMyClass { int d; int GetD() { return d; } void SetD(int nd) { d = nd; } }
The above code means that you can declare a class object using the default constructor:
// It works because the class does not implement any constructors CMyClass mc = new CMyClass();
If you add at least one other constructor to the body of the CMyClass class (for example, a constructor with one parameter), then the default constructor will not automatically be generated
class CMyClass { int d; // declaration of a constructor with 1 parameter, // the default constructor is no longer automatically generated CMyClass(int nd) { d = nd; } int GetD() { return d; } void Set(int nd) { d = nd; } }
After the above implementation, you can not declare an object using the default constructor. However, you can declare an object using a constructor with 1 parameter
// compilation error, because another constructor has already been declared in the class // CMyClass mc = new CMyClass(); CMyClass mc2 = new CMyClass(7); // it works
As a result of the above lines, a compilation error will occurs:
The constructor CMyClass() is undefined
In order to have a default implementation of the constructor and declare a class object using the default constructor, it must be explicitly defined. This can be as follows
class MyClass { int d; // explicit declaration of the default constructor CMyClass() { d = 0; } // declaration of a constructor with 1 parameter CMyClass(int nd) { d = nd; } int GetD() { return d; } void Set(int nd) { d = nd; } }
After such implementation, you can create an instance of the class using two constructors, for example
CMyClass mc = new CMyClass(); // the default constructor is called mc.d = 25; CMyClass mc2 = new CMyClass(5); // the constructor with 1 parameter is called
3. Calling constructors from other constructors. Example
The Java programming language allows you to call the constructors of a class from another constructor of the same class. For this, the ‘this’ keyword is used, which is a reference to the current class.
Example. The example demonstrates the use of the CPixel class, which implements a pixel on the monitor screen.
// A class that implements a pixel on the monitor screen public class CPixel { // internal variables of class private int x, y; // coordinates of pixel private int color; // a color of pixel // constructor without parameters (default constructor) CPixel() { x = y = color = 0; } // constructor with 2 parameters CPixel(int _x, int _y) { x = _x; y = _y; color = 0; } // constructor with 1 parameter CPixel(int _color) { color = _color; x = y = 0; } // constructor with 3 parameters, which calls the constructor with 2 parameters CPixel (int _x, int _y, int _color) { // call the constructor with 2 parameters: only the first operation and only once is called this(_x, _y); //this(_color); // second call of the constructor is forbidden this.color = _color; // this is correct } // access methods int GetX() { return x; } int GetY() { return y; } int GetColor() { return color; } }
Using the CPixel class in another code (method)
... CPixel cp1 = new CPixel(2,8); // calling the constructor with 2 parameters CPixel cp2 = new CPixel(3,5,8); // calling the constructor, which calls another constructore int d; d = cp1.GetX(); // d = 2 d = cp2.GetColor(); // d = 8 d = cp2.GetY(); // d = 5 ...
4. What constraints (requirements) are imposed on calling other constructors from the class constructor?
To correctly call other constructors from the class constructor, you must adhere to the following requirements (restrictions):
- only one other class constructor can be called. Call two or more other constructors of this class is prohibited. This follows from the logic that the class constructor is intended to create a class object only once (rather than two or more times);
- calling another constructor must be the first operation in the calling constructor. If the calling constructor calls another constructor to implement the second (third, etc.) operation, the compiler will generate an error.
5. Can be constructor called from the usual class method?
No, it can not be called. A class constructor can only be called from another constructor of the same class. The main purpose of the class constructor is to create instances (objects) of the class. The class methods have not these privilegies.
Related topics
- Constructors. Parameterized constructors. The keyword ‘this’. Garbage collection. The finalize()method. Examples
- Overloading of methods in classes. Overloading of constructors