Open In App

Can We Define a Method Name Same as Class Name in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

We can have a method name same as a class name in Java but it is not a good practice to do so. This concept can be clear through example rather than explanations. In the below example, a default constructor is called when an object is created and a method with the same name is called using obj.Main().

Example 1:

Java




// Java program to demonstrate that a method
// can have same name as a Constructor or
// class name
 
import java.io.*;
 
public class Main {
    void Main()
    {
        System.out.println(
            "My name is same as Constructor name!");
    }
    public static void main(String[] args)
    {
          // create an object of class
          // Main
        Main obj = new Main();
           
          // call the method
        obj.Main();
    }
}


Output

My name is same as Constructor name!

Example 2: To check whether it’s really showing constructor property or not we can check like this.

Java




// Java program to demonstrate
// checking whether a method is acting like a
// constructor or just a method
 
import java.io.*;
 
public class Main {
 
    // default constructor
    Main() { this(5); }
    /*
      Main(int a) {
        System.out.println(a);
      }
    */
 
    // Just a method
    // not a parameterized constructor
    void Main(int a)
    {
        System.out.println("I am a method");
    }
   
    public static void main(String[] args)
    {
        // create an object
        Main obj = new Main();
           
          // obj.Main();
    }
}


Error:

error: constructor Main in class Main cannot be applied to given types;
    Main() { this(5); }
             ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
1 error

 

Here we don’t call the void Main(int) through an object, but we call through this(int) and its showing error so this cannot be a constructor. This is going to give you an error showing that no parameterized constructor is available, but we think that we have it already that is void Main(int a). But void Main(int a) is not acting like that to prove that just remove the commented section in above code then only it’s going to work.

Java




// Java program to demonstrate that
// constructor is different from method
 
import java.io.*;
 
public class Main {
 
    // default constructor
    Main() { this(5); }
 
    // parameterized constructor
    Main(int a) { System.out.println(a); }
 
    // method but not a constructor
    void Main(int a)
    {
        System.out.println(
            "I am just a method, not  a constructor");
    }
 
    public static void main(String[] args)
    {
        Main obj = new Main();
        // obj.Main();
    }
}


Output

5

Example 3: 

Java




// Java program to demonstrate
// checking whether a method is acting like a
// constructor or just a method
 
import java.io.*;
 
public class Main {
    // default constructor
    Main() { System.out.println("Hey"); }
 
    // method, not a constructor
    void Main()
    {
        System.out.println("I can have return type too.");
    }
 
    public static void main(String[] args)
    {
 
        // create an object
        Main obj = new Main();
    }
}


Output

Hey

Finally, we can conclude that when we have a return type for the methods with the name same as a class name then that loses the features the constructors hold and that will behave like a method. And this is a bad practice in programming.



Last Updated : 07 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads