What is a method signature? [duplicate] - java

This question already has answers here:
Definition of a method signature?
(7 answers)
Closed 7 years ago.
I read a book titled 'Object First with Java' and in page 7 the author mentioned that the method signature "provides information needed to invoke that method". And the the author gave the following example:
void moveHorizontal(int distance)
However, today when I was watching a video about C# on Pluralsight, the author said that "the return type of a method is not part of the method signature".
I'm confused now and would like know what is a method signature?

A method-signature is the part of the method based on which you overload / override it. It contains :
The method name.
The arguments passed to it.
It doesn't contain :
Scope / access modifier
return type.

Method signature is used in interfaces and in abstract classes, but we always define the method data type(return type). It will be something invaluable if the return type is not a part of the signature.

Related

Java Methods vs constructors parameter comparison [duplicate]

This question already has answers here:
Methods vs Constructors in Java
(11 answers)
Closed 2 years ago.
actually i'm beginner my questions might be silly And my doubt is what was actual purpose of methods vs constructors in java,
we can pass values in both methods & constructor parameters,
which one is recommended and why??
A Java method is use to perform some action which is also know as Function. Where you can pass parameter. They must have return type.
A Constructor is special method use to initialise the object . Constructor must not have return type. Constuctor name must be same as class name.
Constructor is used to initialize an object whereas method is used to exhibits functionality of an object.
Constructors are invoked implicitly whereas methods are invoked explicitly.
Constructor does not return any value where the method may/may not return a value.
In case constructor is not present, a default constructor is provided by java compiler. In the case of a method, no default method is provided.
Constructor should be of the same name as that of class. Method name should not be of the same name as that of class.

Checking for valid overloading [duplicate]

This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Closed 8 years ago.
I want to know whether this is a valid overloading :
public class OverLoadingTest{
private void callFunction(Object object){
System.out.println("Printing Object");
}
private void callFunction(String string){
System.out.println("Printing String");
}
}
Further more, since someone asked me this question.
If I do like this,
OverLoadingTest test = new OverLoadingTest();
test.callFunction(null);
what will be printed ?
Of course my opinion is that it isn't valid overloading at all.
So no question of the second part.
Please tell me about this with some explanation.
The method with the least generic argument is called. So, in your case, it will be method accepting String
Note : If 2 classes are at the same level, then you will get an ambiguous call exception. For example if one method took String and another took Exception.
If more than one member method is both accessible and applicable to a method
invocation, it is necessary to choose one to provide the descriptor for
the run-time method dispatch.
The Java programming language uses the rule that the most specific method is chosen.
See more details in JSL 15.12.2.5
In your case, String method will be invoked, if argument is String or null and for other argument's types Object method will be invoked.
In your example, if you define one more method with argument type that is not String (e.g Integer), can't compile the source as it is ambiguous to be invoked between the methods with String and Integer as they are same level.

RuntimeExtension.class -- What does the ".class" mean? [duplicate]

This question already has answers here:
What does .class mean in Java?
(8 answers)
Closed 8 years ago.
I've read the docs for the Junit ExpectedException class, and the parameter for the .expect() method seems to be implemented strangely in all the examples I've studies. When you pass in the expected exception type, it has a .class extension that doesn't appear to be a method of any kind.
Can someone please explain this syntax? Thank you. Example code is below:
public ExpectedException thrown = ExpectedException.none();
public void testPlusException() {
thrown.expect(RuntimeException.class);
Vector testVector = vector1.plus(vector3);
}
Calling .class on a type gets the class object of the particular type. You can read more about the specifics here.
http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html
Although it's not mentioned in the Java documentation for the Object class, all classes derived from Object have a public static Class<T> class member that stores the runtime type of said object.
It's also what gets returned when you call getClass() on an object, keeping in mind that all methods in Java are virtual.

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

This question already has answers here:
Methods With Same Name as Constructor - Why?
(7 answers)
Closed 9 years ago.
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(....){
}
}
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.

Invoking a method by its name [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I invoke a Java method when given the method name as a string?
I have 10 methods named: m1, m2, m3,...
like this:
public void m1(){
..
}
How do I invoke them with string in a 'for' loop?
I want to do this:
for (int i=1;i<11;i++){
invoke('m'+i);
}
You need to use reflection to achieve this.
Method method = getClass().getMethod(methodName);
method.invoke(this);
So, you need to store your method names in an array and use this code piece to call those methods one by one.
You can do this with reflection.
However, I would be interested in your use case. Often it is possible to refactor an application, so that the use of reflection is superfluous.
Use java reflection on this object.

Categories