Monday , December 18 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


Also See:   Java program for reversing a number

constructor in java

Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

Leave a Reply

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