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

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 explanation. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demostrate 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();
    }
}

chevron_right


Output

5

Example 3: 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// checking whether a method is acting like a
// constrcutor 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();
    }
}

chevron_right


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.

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.