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
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 error0
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 error1
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 error2
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 error3
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 error4
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 error2
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 error6
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 error7
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 error8
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 error9
50
51
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 error2
53
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 error2
public
56
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 error3
58
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 error2
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 error6
Hey1
Hey2
Hey1
Hey4
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 error7
Hey6
Hey7
Hey8
Hey1
Hey1
// Java program to demonstrate that a method
1error: 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 error7
// Java program to demonstrate that a method
3error: 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 error2
53
53
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 that a method
7
// Java program to demonstrate that a method
8
// Java program to demonstrate that a method
9
import
java.io.*;
public
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 error0
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 error1
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 error2
// can have same name as a Constructor or
6error: 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 error2
// can have same name as a Constructor or
8// can have same name as a Constructor or
9// class name
0// class name
1// class name
2error: 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 error2
// class name
4// class name
5// class name
6
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 error7
// class name
8// class name
5
53
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 error2
import
2
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 error2
import
4error: 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 error2
import
6error: 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 error2
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 error3
import
9java.io.*;
0 java.io.*;
1error: 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 error2
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 error6
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 error7
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 error8
java.io.*;
651
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 error2
53
public
0
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 error2
public
56
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 error3
58
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 error2
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 error6
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 error7
public
9error: 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 error7
Hey6
Hey7
Hey8
Hey1
Hey1
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 error06
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 error2
53
53
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
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 error10
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 error11
import
java.io.*;
public
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 error0
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 error1
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 error2
// can have same name as a Constructor or
6error: 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 error2
// can have same name as a Constructor or
8// can have same name as a Constructor or
9// class name
0// class name
1// class name
2
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 error2
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 error26
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 error2
import
9java.io.*;
0 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 error30
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 error2
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 error32
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 error2
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 error3
import
9java.io.*;
0 java.io.*;
1error: 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 error2
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 error6
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 error7
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 error8
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 error9
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 error43
51
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 error2
53
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 error2
public
56
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 error3
58
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 error2
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 error6
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 error7
Hey6
Hey7
Hey8
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 error7
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 error06
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 error2
53
53
Output
5
Example 3:
Java
// Java program to demonstrate that a method
7
// Java program to demonstrate that a method
8
// Java program to demonstrate that a method
9
import
java.io.*;
public
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 error0
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 error1
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 error2
// can have same name as a Constructor or
6error: 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 error2
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 error74
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 error75
// class name
2
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 error2
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 error78
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 error2
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 error3
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 error4
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 error2
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 error6
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 error7
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 error8
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 error86
51
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 error2
53
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 error2
public
56
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 error3
58
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 error2
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 error6
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 error7
public
9error: 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 error7
Hey6
Hey7
Hey8
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 error2
53
53
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.