A constructor is automatically called when a new instance of an object is created. A constructor is a special technique to initialize the objects. It is created with the same name as class name but it doesn't have any return type
For example :
public class MyClass1{ //This is the constructor with the same name. MyClass1(){ } .. .. }
Purpose: A constructor is used for initializing of object and java method is used to expose the behaviour of an object.
Return type: constructor doesn't have any return type but java method must have a return type.
Invoke method: constructor is invoked implicitly whereas java method is invoked explicitly.
Compiler: Java compiler can generate default constructor if no constructor is found in the program. But in the case of java method, method Is not provided by the java compiler.
Name: Constructor name must be same as the class name. but it not necessary in java method.
Follow Us