Make Selenium Easy

Difference Between Constructor and Method in Java

Let’s start with very basic definitions of both Constructor and Methods in Java:-

A constructor of a class is used to initialize the object or instance of class using new keyword. It can be called as special method as it is different from normal methods. It differs from methods as a constructor should have same name as class name and should not have any return type.

A method is a set of code with name to perform specific actions whenever and wherever needed. A method can accept parameters and return value.

Now see the differences between Constructor and Method:-

  1. Constructor is used to initialize the object or instance of class whereas a method is used to perform specific task or functionality.
  2. Constructor must have same name as class name whereas a method can have any valid name. A method can have name same as class name with a return type.
  3. Constructor must have no return type (Not even void) in declaration whereas a method should have a return type (Including void) in declaration.
  4. A constructor can be called explicitly (Parameterized constructor) or implicitly ( No arg constructor) whereas a method needs to be called explicitly always.
  5. If a class does not have any constructor, a default constructor is provided. There is no default method concept. You need to create it explicitly.
  6. Constructors are not inherited while non-static methods are inherited.
  7. Constructors cannot be overridden whereas methods can be overridden.
  8. A constructor can be called using new keyword whereas method can be called using object (Non static methods ) or class reference ( Static methods) or by name ( Calling Non static method in another non static method and Calling static method in another static method ).
  9. A constructor can call another constructor using this() or super() whereas a method can call other method directly or using class or object reference.
  10. A constructor cannot be static, abstract , final, native or synchronized whereas a method can be.
  11. A constructor can bring method (Non-static) in existence/memory.

#ThanksForReading

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

2 thoughts on “Difference Between Constructor and Method in Java

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Difference Between Constructor and Method in Java

by Amod Mahajan time to read: 1 min
2
%d bloggers like this: