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(....){
}
}
Definition and Usage The void keyword specifies that a method should not have a return value.
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.
Yes, we can define multiple methods in a class with the same name but with different types of parameters.
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.
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.
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.
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.)
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With