Constructor vs Method

In this page, we will see the difference between constructor and method.

Difference between constructor and method in java

There are many differences between constructors and methods. They are given below.

Constructor Method
A constructor is used to initialize the state of an object. A method is used to expose the behaviour of an object that means to perform some task.
Constructor is invoked implicitly at the time of object creation. Method is invoked explicitly by the user when needed.
Constructor name must be same as the class name. Method name may or may not be same as class name.
Constructor must not have a return type. Method must have a return type.
The java compiler provides a default constructor if you don't have any constructor. Method is not provided by compiler in any case.

Example of defining constructor and method in the Java Class.

class Student {
	String studentName;
	String studentAddress;
	String studentNumber;
	int rollNumber;

	// default constructor
	public Student() {
		studentName = "John";
		studentAddress = "Delhi";
		studentNumber = "9836521456";
		rollNumber = 12365485;

	// parameterized constructor
	public Student(String name, String address, String number, int roll) {
		studentAddress = address;
		studentName = name;
		studentNumber = number;
		rollNumber = roll;

	// define method{ void is return type}
	public void printDetail() {
		System.out.println("Name :" + studentName);
		System.out.println("Address :" + studentAddress);
		System.out.println("Number :" + studentNumber);
		System.out.println("Roll Number " + rollNumber);


	//main method
	public static void main(String[] args) {
		Student student1=new Student();
		//creating second object
		Student student2=new Student("Ashutosh ","Lucknow","453535335",454);
Name :John
Address :Delhi
Number :9836521456
Roll Number 12365485
Name :Ashutosh 
Address :Lucknow
Number :453535335
Roll Number 454
woman avatar