Thursday , September 21 2017
Home / JAVA / Constructor in Java

Constructor in Java

In Java Constructor is a block of code which gets executed automatically when object of that class is created. It is not a function so does not return any value. Name of a class and a constructor should be same. Constructor and functions are same only with difference that constructor runs automatically after the initialization of object. Constructor can be called only once when the object is created.

class Constructor  // class name is constructor
{
Constructor()    // constructor is defined which automatically executed after initialization. 
{
System.out.println("Default Constructor run automatically after the object has been created");
}
void Function1()  // * Explained below
{
System.out.println("Function will run after calling from object"); 
}
public static void main(String args[]) // main function is defined
{
Constructor obj=new Constructor();    // ** Explained below
Obj.Function1();  //  function is being called from object than it will run
}  // end of main
} //end of class

OUTPUT:

constructor in java

Check Also

Program to check Upper Triangular matrix

What is Upper Triangular Matrix?  An upper triangular matrix is a square matrix in which ...

Leave a Reply

Your email address will not be published. Required fields are marked *