Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write parameter names in method call

Tags:

java

I would like to write down the names of parameter methods while calling a method, not just giving the parameter itself. Example:

public exampleMethod(boolean xWasDone) {}

The known way of calling this is:

exampleMethod(true);

Is there a way to call it with explicitly naming the parameter? Something like this?:

exampleMethod(xWasDone: true);

I consider the latter much more readable and would thereby be interested in a correct way of writing my calls like that.

like image 301
paulamoskreiner Avatar asked Nov 11 '15 14:11

paulamoskreiner


People also ask

Can we pass method name as parameter in Java?

Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses.

Which method is used to get all the parameter names?

You can obtain the names of the formal parameters of any method or constructor with the method java. lang.

Can you call a method in a parameter?

There are two ways to call a method with parameters in java: Passing parameters of primtive data type and Passing parameters of reference data type. 4. in Java, Everything is passed by value whether it is reference data type or primitive data type.

What is named parameter in C#?

Named parameters provides us the relaxation to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. Using named parameters in C#, we can put any parameter in any sequence as long as the name is there.


1 Answers

This syntax is not supported in Java.

It can be argued that it is not needed as the order of parameters in function signatures are strongly enforced in Java.

Also, you should be mindful that function overloading is supported in Java, where multiple functions may have the same name but different number of parameters. This means you cannot omit any specified input, in order to avoid calling another overloaded function unintentionally.

like image 167
Ling Zhong Avatar answered Sep 28 '22 07:09

Ling Zhong