What are the differences between the constructors and methods?

Constructor:

  • A constructor is used to initialize the state of an object.
  • A constructor is invoked implicitly.
  • A constructor must not have a return type.
  • The Java compiler provides a default constructor if you don't have any constructor in a class.
  • The constructor name must be same as the class name.

Method:

  • A method is used to expose the behavior of an object.
  • A method must have a return type.
  • The method is invoked explicitly.
  • The method is not provided by the compiler in any case.
  • The method name may or may not be same as class name.

Constructor:

  • A constructor is used to initialize the state of an object.
  • A constructor is invoked implicitly.
  • A constructor must not have a return type.
  • The Java compiler provides a default constructor if you don’t have any constructor in a class.
  • The constructor name must be same as the class name.

Method:

  • A method is used to expose the behavior of an object.
  • A method must have a return type.
  • The method is invoked explicitly.
  • The method is not provided by the compiler in any case.
  • The method name may or may not be same as class name.
Sriram Mannava
Sriram Mannava

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity.

Leave a Reply

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