OOP

Difference between Method and constructor in Java

Difference between Method and constructor in Java

In this tutorial, we will discuss the difference between Method and constructor in Java language. This is a very important concept in Java for students or programmers.

 

Now, we will get to know about the Java method

Method

  • The method does not need the name similar to the class
  • Java compiler does not provide any default or any type of methods in Java in any case
  • The method is used to expose the behaviour of an object – to perform a special task or operation
  • The method must have a return type and whereas the method may/may not return a value.
  • The method has to be called explicitly with the class reference or object reference using dot(.) operator
  • Java method can be a final, abstract, static, and synchronized.
Method in java

 

Example

In this example, we create some methods including static method and normal method and call those methods

public class StuDetails{

static void demoMethod(){ //static method
   System.out.println("This is a demo method");
}

static void method(){
   System.out.println("This is a method");
}

void displayMethod(){
   System.out.println("This is display method");
}

public static void main (String args[]){
demoMethod(); // calling static metod

StuDetails.method();// calling static metod using class reference

StuDetails s=new StuDetails();

s.displayMethod();// callingmetod using object reference

}

}

When the above code is compiled and executed, it produces the following results

This is a demo method
This is a method
This is a display method

 

Now, we will get to know about the Java constructor

Constructor

  • The constructor must be of the same name of that class
  • The constructor is used to initialize the state of an object – to place a value into data member of a class
  • The constructor must not have any return type. whereas it never returns value
  • The constructor will be automatically executed whenever the object is created
  • Java constructor cannot be a final, abstract, static, and synchronized
  • Default constructor provided by java compiler, when we do not have any constructor in Java program
Constructor in Java

 

Example

In this example, we create default and parameterized constructor and call those constructors using create an object

Program 1

public class EmployeeDet
{
int empNo;//global variable
String empName;

EmployeeDet()//default constructor
{
System.out.println("I am a default constructor");
}

EmployeeDet(int empNo, String name){//parameterized constructor
System.out.println("I am a parameterized constructor");
this.empNo=empNo;//assign local variable to global variable
empName=name;
System.out.println("\nEmployee name is: "+name);
System.out.println("Employee number is: "+empNo);
}
public static void main(String args[]){
    EmployeeDet e=new EmployeeDet();
    //default contructor called automatically when it is created
    EmployeeDet e1=new EmployeeDet(121,"Ragulan");
    //parameterized contructor called automatically when it is created
    
}
}

When the above code is compiled and executed, it produces the following results

I am a default constructor
I am a parameterized constructor

Employee name is: Ragulan
Employee number is:121

 

Suggested for you

Method in Java

Constructor in Java

Method overloading in Java programming language
Recursion in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago