Default constructor in java, constructor present in another class

Java Programming Language / Class, Object and Methods in java

1040

Program:

 // Here is a simple example that uses a constructor without parameters
class ConstructorClass {
   int x;

   // Following is the constructor
   ConstructorClass() {
      x = 10;
   }
}

// You will have to call constructor to initialize objects as follows 
public class MainClass {

   public static void main(String args[]) {
      ConstructorClass t1 = new ConstructorClass();
      ConstructorClass t2 = new ConstructorClass();
      System.out.println(t1.x + " " + t2.x);
   }
}

Output:

10 10
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.