implements Value<T...>

S

Stefan Ram

How can I modify the end of line 6 to make the declaration of
the class »Value1« being compiled (similar to the declaration of
the class »Value0«, which is being compiled)?

interface Value<T> { public void operation(final T t); }

class Value0<T> implements Value<T[]>
{ public void operation( final T[] t ){}; }

class Value1<T> implements Value<T...>
{ public void operation( final T... t ){}; }

/* Main.java:6: > expected
class Value1<T> implements Value<T...>
1 error ^ */

~~

Ok, the following class declaration now can be compiled:

class Value1<T> implements Value<T[]>
{ public void operation( final T... t ){}; }

But I still get a warning:

Main.java:6: warning: operation(T...) in Value1 implements operation(T) in Value; overridden method has no '...'
{ public void operation( final T... t ){}; }
^
1 warning

Is there a way to write this in such a way that there will
not even be a warning message, retaining the declaration
»public void operation( final T... t ){};«?
 
J

Joshua Cranmer

Stefan said:
But I still get a warning:

Main.java:6: warning: operation(T...) in Value1 implements operation(T) in Value; overridden method has no '...'
{ public void operation( final T... t ){}; }
^
1 warning

Is there a way to write this in such a way that there will
not even be a warning message, retaining the declaration
»public void operation( final T... t ){};«?

@SuppressWarnings? The (javac) source code says it's an "overrides" warning.
 
A

Andreas Leitgeb

Joshua Cranmer said:
@SuppressWarnings? The (javac) source code says it's an "overrides" warning.

Note further, that unless you explicitly asked javac to print those
warnings, it wouldn't have bothered.
The reason for the warning is, that if you had:
Value<Object[]> v = new Value1<Object>();
then you cannot do:
v.operation("Hello", "World");
but only:
((Value1<Object>)v).operation("Hello", "World");

I'm not at all surprised that Value<T...> wasn't directly
allowed. To use the varargs-feature for a method, the method
requires the "transient"-flag (I mean the bit which for fields
means transient) having been set. Clearly, the Value.class file
will not have that flag set for "operation", and merely using
it with some type cannot modify the template's class-file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top