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
}
}
import java.io.*;
class Geek {
int num;
String name;
// This would be invoked while an object
// of that class created.
Geek()
{
System.out.println("Constructor called");
}
}
class GFG {
public static void main(String[] args)
{
// this would invoke default constructor.
Geek geek1 = new Geek();
// Default constructor provides the default
// values to the object like 0, null
System.out.println(geek1.name);
System.out.println(geek1.num);
}
}
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
}
}
import java.io.*;
class Addition {
int sum = 0;
public int addTwoInt(int a, int b)
{
// Adding two integer value.
sum = a + b;
// Returning summation of two values.
return sum;
}
}
class GFG {
public static void main(String[] args)
{
// Creating an instance of Addition class
Addition add = new Addition();
// Calling addTwoInt() method
// to add two integer
// using instance created
// in above step.
int s = add.addTwoInt(1, 2);
System.out.println("Sum of two "
+ "integer values: "
+ s);
}
}
Output
Sum of two integer values: 3
Constructors Vs Methods
| Basis | Constructor | Method |
|---|---|---|
| Purpose | Used to initialize objects of a class | Used to perform specific operations or define object behavior |
| Name | Must have the same name as the class | Can have any valid name |
| Return Type | Does not have any return type | Must have a return type or void |
| Calling | Automatically called when an object is created | Called explicitly using the object name or class name |
| Execution | Executes only once when an object is created | Can be called multiple times whenever required |
| Inheritance | Cannot be inherited by subclasses | Can be inherited by subclasses |
| Overloading | Supports constructor overloading | Supports method overloading and method overriding |
| Purpose of Use | Initializes instance variables and sets object state | Performs calculations, operations, and other tasks |
| Modifiers | Cannot be static, final, or abstract | Can use access modifiers like static, final, and abstract |
| Syntax | Uses class name followed by parameters | Uses return type, method name, and parameters |