Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object parameter not accepting string

Tags:

java

I have a method with following signature -

public void errorNew(final int a, final String key, final Object... params);

And I am trying to call it as -

errorNew(1, "a", "vb", "df");

But eclipse is showing me error -

The method errorNew(int, String, Object[]) in the type Logger is not applicable for the arguments (int, String, String, String)

Any idea about the reason?

When I try to build using maven, it shows following error -

 method errorNew in interface Logger cannot be applied to given types;
[ERROR] required: int,java.lang.String,java.lang.Object[]
[ERROR] found: int,java.lang.String,java.lang.String,java.lang.String
[ERROR] reason: actual and formal argument lists differ in length
like image 408
N.. Avatar asked Mar 23 '14 21:03

N..


People also ask

Can we pass string as a parameter in Java?

String objects are immutable in Java; a method that is passed a reference to a String object cannot change the original object.

Can you use an object as a parameter in Java?

In Java, we can pass a reference to an object (also called a "handle")as a parameter. We can then change something inside the object; we just can't change what object the handle refers to.

What is an object parameter?

The Parameter Object is simply a wrapper object for all parameters of a method. It is nothing more than just a regular POJO. The advantage of the Parameter Object over a regular method parameter list is the fact that class fields can have default values.

How to pass parameter in Java method?

Parameters and Arguments 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. You can add as many parameters as you want, just separate them with a comma.


1 Answers

This is a variable-arity method:

public void errorNew(final int a, final String key, final Object... params);

and this is a fixed-arity method:

public void errorNew(final int a, final String key, final Object[] params);

A variable-arity method is applicable if the number of actual arguments is equal or greater to n - 1, where n is the number of parameters declared by the method. A fixed-arity method is only applicable if the number of arguments is exactly equal to the number of declared parameters (see the specs)

So the difference is not at the bytecode level, but at the compiler stage: when the compiler has to resolve the method signature, the algorithm changes as I described above, depending on the declaration being a fixed- or variable-arity one. That's why the compiler doesn't complain when your LoggerImpl implements the Logger interface using a vararg parameter, but can't find the method when you call it on an instance of the Logger interface (method signatures are resolved statically at compile-time, not dynamically at runtime, depending on the actual type of the object)

like image 157
Raffaele Avatar answered Oct 02 '22 14:10

Raffaele