Can I name a method with the same name of the class in Java?

Can a class have a method with the same name of the class in Java?
The answer is yes. See the example below.

       
Public class MyClass {
 public MyClass() {
 
 }
 public void MyClass() {
         System.out.println("I have the same name as the class!");
 }
}

       

You could definitely name a method with the name of the class. As a rule of thumb, you should not capitalize the first letter of the name of your method, though.

Comments

Popular posts from this blog

What is new in Java 8

Quicksort Java implementation