Can We Define a Method Name Same as Class Name in Java?
source link: https://www.geeksforgeeks.org/can-we-define-a-method-name-same-as-class-name-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
- Last Updated : 24 Oct, 2020
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 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();
}
}
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 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 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();
}
}
5
Example 3:
// 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();
}
}
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 Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK