简体   繁体   中英

Java compilation error with varargs

I created a library with the following method :

protected static int foo(String strParam, Object... params)

I link this library to my application and I call the method :

foo("a","b")

but it does not compile, I get the following error message : actual argument String cannot be converted to Object[] by method invocation conversion.

any idea ?

Works for me:

public class XXX extends Lib {
    public static void main(String[] args) {
        foo("a", "b");
    }
}

class Lib{
    protected static int foo(String str, Object... args) {
        return 42;
    }
}

So:

  • What JDK version are you using?
  • What source/class version of Java are you using? Anything below "5" is useless.
  • Are there other methods called foo somewhere (ie overloaded methods)?
  • Are you sure you don't "link" (whatever that means to you) against an old version of your code?

The method is protected . Is the call occurring in a class in the same package, or in a subclass? If not, the caller cannot see this definition of foo . I don't see why it would result in that particular error though. This might be evidence that the compiler is trying to match this up with a different method signature.

Is your call really just foo( ... ) ? I would expect to see the name of the containing class there, since it is a static method -- ie LibraryClass.foo( ... ) . If you are not qualifying the method name, then it should be looking in the local class scope for a matching declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM