VOOZH about

URL: https://www.geeksforgeeks.org/java/difference-between-the-constructors-and-methods/

⇱ Difference between the Constructors and Methods - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference between the Constructors and Methods

Last Updated : 16 Jun, 2026

In Java, constructors and methods are important components of a class that help in initializing objects and performing specific operations. A constructor is used to initialize an object when it is created, while a method defines the behavior and functionality of an object.

  • Both constructors and methods improve code reusability and program organization.
  • Java supports constructor and method overloading for multiple implementations.

Constructor

A constructor in Java is a special method used to initialize objects of a class. It is automatically called when an object is created and assigns initial values to object variables. Constructors help in setting up the initial state of an object.

  • Constructor has the same name as the class and does not have a return type.
  • It is automatically executed when an object is create

Syntax

class ClassName {
ClassName() {
// initialization code
}
}


Output
Constructor called
null
0

Method

A method in Java is a block of code that performs a specific task or operation. Methods define the behavior of objects and help in improving code reusability. They execute only when they are called by the program.

  • Methods contain a return type, method name, and parameters.
  • They help in code reusability and modular programming.

Syntax

class ClassName {
returnType methodName(parameters) {
// method body
}
}


Output
Sum of two integer values: 3

Constructors Vs Methods

BasisConstructorMethod
PurposeUsed to initialize objects of a classUsed to perform specific operations or define object behavior
NameMust have the same name as the classCan have any valid name
Return TypeDoes not have any return typeMust have a return type or void
CallingAutomatically called when an object is createdCalled explicitly using the object name or class name
ExecutionExecutes only once when an object is createdCan be called multiple times whenever required
InheritanceCannot be inherited by subclassesCan be inherited by subclasses
OverloadingSupports constructor overloadingSupports method overloading and method overriding
Purpose of UseInitializes instance variables and sets object statePerforms calculations, operations, and other tasks
ModifiersCannot be static, final, or abstractCan use access modifiers like static, final, and abstract
SyntaxUses class name followed by parametersUses return type, method name, and parameters
Comment