Home »
Java programming language
Differences between constructors and methods in Java
Java constructors vs methods: Here, we are going to learn what are the differences between constructors and methods in Java? Compare Constructor v/s methods in java? And, how constructor differs from methods in java?
By Preeti Jain Last updated : January 14, 2024
Quick Answer
Java constructor is used to initialize a newly created object and invoke it when an object is being created. Whereas, a Java method returns a value upon its execution and calls with/without an object based upon its definition.
Java Methods
- A method is used to explore the behavior of an object.
- We can prefix access modifiers with methods.
- A method must have a return type like a void, any primitives type (int, char, float, etc.), any Object type(Integer, Float, String, etc).
- Java compiler does not call method implicitly we need to call explicitly.
- Let suppose if we have a class and in that class, we don't have any single method so in that case java compiler does not include any method from its own by default.
- It is not mandated to have the same name of methods as its class name or in other words, method name may or may not be same as of class name.
-
Methods are of two types:
- No argument methods
- Parameterized methods
Java No argument method
A method that has no parameter or argument or in other words this type of method does not take any argument is known as the no-argument method.
Example
class NoArgumentMethod {
// No argument method definition
void display() {
System.out.println("We are in no argument method");
}
}
class Main {
public static void main(String[] args) {
// creating an object of NoArgumentMethod class
NoArgumentMethod nam = new NoArgumentMethod();
// calling display() method
nam.display();
}
}
Output:
E:\Programs>javac Main.java
E:\Programs>java Main
We are in no argument method
Java parameterized method
A method that has parameters or in other words this type of method takes any argument is known as a parameterized method.
Example
class ParameterizedMethod {
String name;
// Parameterized method definition
void display(String name) {
System.out.println("The name is " + name);
}
}
class Main {
public static void main(String[] args) {
// creating an object of ParameterizedMethod class
ParameterizedMethod pm = new ParameterizedMethod();
// calling display() method
pm.display("Rahul");
}
}
Output
E:\Programs>javac Main.java
E:\Programs>java Main
The name is Rahul
Java Constructors
- A constructor is a special method which calls implicitly by JVM(Java Virtual Machine) at the time of object creation.
- The purpose of a constructor is to initialize an instance variable of the object.
- We can prefix access specifiers with constructor also.
- A constructor must not have a return type i.e. it does not return anything.
- A constructor is a special function which calls implicitly(i.e. it is good for the user that we don't need to call explicitly).
- Let suppose we have a class named "Constructor" and in that class, we don't include any constructor so in that case java compiler will include a default constructor from its own.
- It is mandated to have the same name as the constructor as its class name or in other words, constructor name must be same as the class name.
-
We have three types of constructors:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
Default constructor
This default constructor calls implicitly by the compiler if we don't include any constructor in the class. Default constructor has no parameter or in other words no-argument constructor.
Example
class DefaultConstructor {
// Constructor defined
DefaultConstructor() {
System.out.println("We are in default constructor");
}
}
class Main {
public static void main(String[] args) {
// Calling Default constructor
DefaultConstructor dc = new DefaultConstructor();
}
}
Output
E:\Programs>javac Main.java
E:\Programs>java Main
We are in default constructor
Parameterized constructor
The constructor that has parameter is known as parameterized constructor. If we want to initialize instance variable with our values and this constructor does not call by compiler implicitly.
Example
class ParameterizedConstructor {
String name;
// Constructor defined
ParameterizedConstructor(String name) {
this.name = name;
}
}
class Main {
public static void main(String[] args) {
// Calling Parameterized constructor
ParameterizedConstructor pc = new ParameterizedConstructor("Preeti");
System.out.println("The name is :" + pc.name);
}
}
Output
E:\Programs>javac Main.java
E:\Programs>java Main
The name is : Preeti
Copy constructor
A constructor that has one parameter and the parameter is the reference of the same class.
Example
class CopyConstructor {
String name;
// Constructor defined
CopyConstructor(String name) {
this.name = name;
}
}
class Main {
public static void main(String[] args) {
// Calling constructor
CopyConstructor cc1 = new CopyConstructor("Preeti");
// Copied cc1 constructor
CopyConstructor cc2 = cc1;
System.out.println("The value of cc1 is :" + cc1.name);
System.out.println("The value of cc2 is :" + cc2.name);
}
}
Output
E:\Programs>javac Main.java
E:\Programs>java Main
The value of cc1 is :Preeti
The value of cc2 is :Preeti
Difference between Java methods and constructors
The main difference between Java methods and constructors is that a Java constructor is used to initialize a newly created object and invoke it when an object is being created. Whereas, a Java method returns a value upon its execution and calls with/without an object based upon its definition.