R
R
rogiivs2019-05-02 18:11:05
Java
rogiivs, 2019-05-02 18:11:05

How to find out how many parameters are passed to a Java method?

How to find out how many variables are passed to a Java method and what types they are. JS has an arguments pseudo-array . Is there something similar in Java?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2019-05-03
@rogiivs

If varargs is passed to the method (the example is stealthy above)

public static void main(Object... args) {
  System.out.println(args.length);
}

then it is enough to treat args as an array and work with it in the same way.
If we are talking about a method in the general case, then you will need the Reflection API, an object of the corresponding method and its getParameterCount() and getParameterTypes() methods.
But why do you need it? Maybe the problem should be solved differently?

S
Sergey Gornostaev, 2019-05-02
@sergey-gornostaev

First, there are no functions in Java, only methods. Secondly, Java is a language with static typing, which means that even before compilation it is known how many parameters a method has and what types they are.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question