Default Constructor

Total Post:113

Points:791
Java 
 236  View(s)
Ratings:
Rate this:

What Is A Default Constructor ?

  1. Post:140

    Points:984
    Re: Default Constructor

    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:

    // 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);     } }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!