Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java allows method that has class name and type void [duplicate]

Tags:

java

Java allows to create method which has the name of the class and type void ( Like void constructor). Constructor has no type and it do the function of the constructor. But is there any usage above mentioned kind of methods. Can you give examples of those usages

Sample Code:

//my class
class MyClass{

    //constructor
    public MyClass(.....){
    }

    //What is the use of the below method
    public void MyClass(....){
    }
}
like image 273
Malintha Avatar asked Aug 28 '13 06:08

Malintha


People also ask

Why do we use void method in Java?

Definition and Usage The void keyword specifies that a method should not have a return value.

Can a method have same name as class in Java?

Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur.

Can two classes have method with same name?

Yes, we can define multiple methods in a class with the same name but with different types of parameters.

Which is the special method in which class name and method name should be same?

Look up "constructor". That is what this "method with the same name as the class" is. Conceptually, a constructor is not a method, even though it looks a lot like one. Think of a constructor as a special block of code that is called to initialize a new object.


5 Answers

To answer your question: No, it has no special use. In fact, it is counter intuitive and confusing. Some compilers will even generate a warning "This method has a constructor name".

But because technically it is possible that it is not a compilation error, I would advice staying away from it. There are several different method names which can be more descriptive and serve the same purpose.

like image 194
rocketboy Avatar answered Nov 03 '22 00:11

rocketboy


Yes, A fresher to Java may confuse with this. The constructor cannot have a return type. But some people misunderstand that the "no return type" and "void" are some what equal but it is not. Constructor is a different story and the method that has the class name and any other return type (void, String, int, .......) is different. But it is more confusing.

like image 25
Malintha Avatar answered Nov 03 '22 00:11

Malintha


There is no sensible usage for a method those name is the same as the class name.

  • It is a style violation. According to the official Java style guide, names of Java methods should start with a lower case letter.

  • It is confusing because it looks superficially like a constructor.

  • It is confusing because when you use such a method it looks like you are using the classname incorrectly.

  • It is possible that this will result in unexpected behaviour and/or unexpected compilation errors due to the class-name vs method-name ambiguity.


Why java allows method that has class name and type void?

Basically because the Java language does not enforce the identifier style rules. (IMO, it would have been better if it did enforce the rules ... but the decision was made a long time ago, and it can't be changed for compatibility reasons.)

like image 32
Stephen C Avatar answered Nov 02 '22 22:11

Stephen C


No It don't have special usage, it will be treated as similar to other methods inside the class.

It will be worth reading below article:

http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

If the method name is same as class name and it has no return type then its known as constructor which has special usage in oops.

by keeping such names as method it will only create a confusion and code readabilty.

below link will might help you why readibility matters: http://java.dzone.com/articles/why-code-readability-matters

like image 43
Jayaram Avatar answered Nov 03 '22 00:11

Jayaram


The usage is identical to that of any other method. And the return type need not be void. It can often be confusing, but it is perfectly legal to name methods the same as the class name. It'll usually cause more confusion then you want, but it's a legal behavior. The methods have no special properties apart from any other class method.

like image 41
Kon Avatar answered Nov 03 '22 00:11

Kon