If you are going to instantiate an array of objects of the class, the class must have a default constructor. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameter less constructor A::A() .
Syntax:
class X { public:
X(); // Default constructor with no arguments
X(int = 0); // Default constructor with one default argument
X(int, int , int = 0); // Constructor
};
Ex:
// Main.java class Test {
int i;
Test t;
boolean b;
byte bt;
float ft;
}
public class Main {
public static void main(String args[]) {
Test t = new Test(); // default constructor is called.
System.out.println(t.i);
System.out.println(t.t);
System.out.println(t.b);
System.out.println(t.bt);
System.out.println(t.ft);
}
}
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Default Constructor :
If you are going to instantiate an array of objects of the class, the class must have a default constructor. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameter less constructor A::A() .
Syntax:
class X {
public:
X(); // Default constructor with no arguments
X(int = 0); // Default constructor with one default argument
X(int, int , int = 0); // Constructor
};
Ex: