Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make a method that takes any number of arguments?

Tags:

java

I was looking at another post here which was describing what seems to be my problem: How to make a method which accepts any number of arguments of any type in Java?

However, when I attempted to do this method, when I compiled the program it gave me the error "int cannot be converted to java.util.Objects"

What am I doing wrong?

Code:

public static void clearArray (Objects... args)
{
    System.out.println("Error, non character value");
}

How I called the function:

import java.util.Objects;
// Stuff...
clearArray(1);
// Other stuff...

Thank you for checking out my problem!

like image 508
Riley Carney Avatar asked Dec 10 '22 20:12

Riley Carney


2 Answers

Look at the signature

public static void clearArray (Objects... args)

That method receivng Objects type and you are passing integer to it. Perhaps changing that to

public static void clearArray (Object... args)

Helps.

like image 144
Suresh Atta Avatar answered Jan 04 '23 23:01

Suresh Atta


You want java.lang.Object, not java.util.Objects.

java.util.Objects is a class with utility methods, not a class you can actually extend and instantiate.

java.lang.Object on the other hand is the superclass of all objects in Java.

And even in a multi-param (varargs) method, the signature needs to be Object ..., not Objects ....

like image 34
Sean Patrick Floyd Avatar answered Jan 05 '23 00:01

Sean Patrick Floyd