How to call method with Object... as parameter

passing object as parameter in java example
how to pass class object as parameter in java
how to call a method with parameters in java
passing object as parameter in java program
returning objects in java
method arguments
java modify passed parameter
passing array of objects as arguments in java

I wonder how can I call a class method which are expecting Object...(Object[]) as parameter.

I can't know at compilation time now many parameters I need to pass to it, so I can't do like somemethod(1,2,3,4,5).

I'm doing big construction like:

if (param.lengts()==5) {
somemethod(1,2,3,4,5);
} elseif (param.lengts()==4) 
somemethod(1,2,3,4);
....

I was trying to pass List<> and ArrayList<> but without success. Is there a simple way how to convert my dynamic array to the method? I can't change method constructor.

Problem about to call the method, not with declare or read parameters inside the method.


You can use variable arguments for this:

private void somemethod(Integer.. array) {
}

And call it like this:

somemethod()    
somemethod(1)    
somemethod(1,2)

If you have an arraylist as an input, you can pass it as this:

someMethod(list.toArray(new Integer[list.size()]);

How do I pass objects as parameters in java?, Can we pass an object as parameter to call a method in Java? When we call a method with passing the reference of an object by value, Java does not copy the object into the memory. Actually, it copies the reference of the object into the memory and passes the copy to the parameters of the called method.


Pass an array.

Object[] params = ... build the array from the args 
somemethod(params);
...
void somemethod(Object... objs); 

Using Objects (The Java™ Tutorials > Learning the Java Language , How do you call a method from an object in Java? 1). The method is basically equal to a function in C, which is used for reusing code. It is comprised of a set of statements, and the method itself can be invoked through another statement. When it is called or invoked, all the statements which form a part of the method are executed.


You can use var args:

method declaration:

void somemethod(Object ...){ }

And call this methodo with how many parameters you need.

How to call method with Object as parameter, You can use variable arguments for this: private void somemethod(Integer.. array​) { }. And call it like this: somemethod() somemethod(1)  If the method someMethod required two parameters, int a and Object obj, it would look like " someMethod(int a, Object obj) ". To use this new method, it would be called by the method name followed by an integer and an Object in parentheses: someMethod(4, thing) where thing is an Object.


Java passing object as parameter - Java Tutorials, We can pass Object of any class as parameter to a method in java. you how to pass instance variables of particular object to calling method. A parameter is a value that you can pass to a method in Java. Then the method can use the parameter as though it were a local variable initialized with the value of the variable passed to it by the calling method. The guessing-game application has a method named getRandomNumber that returns a random number between 1 and 10:


4. Methods Use Instance Variables: How Objects Behave, Most importantly, if a method has parameters, you must pass arguments of the right type and order. Calling a two-parameter method, and sending it two arguments. The call() allows for a function/method belonging to one object to be assigned and called for a different object. call() provides a new value of this to the function/method. With call() , you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.


Java Method Arguments, When Java calls a method, it makes a copy of its actual parameters and sends the copies to the Now we can pass the object as a parameter to a method: Calling a query method and passing its results as the parameters of another method, while that method could call the query directly. Solution. Instead of passing the value through a parameter, try placing a query call inside the method body.