I want to define a time interface like that:
public interface TimeInterface<T>
{
static T ZERO;
static T INFINITY;
// some other methods...
}
Is this possible, or how to do that to avoid errors?
Thanks in advance!
I want to define a time interface like that:
public interface TimeInterface<T>
{
static T ZERO;
static T INFINITY;
// some other methods...
}
Is this possible, or how to do that to avoid errors?
Thanks in advance!
The only
static
thing that can contain type-parameters is thestatic
method, which must define it's own type-parameters. Something like:In your case, the type-parameter has a class scope, it's instance-bound and has nothing to do with the
static
members of the class.So, you should either remove the
static
keywords forZERO
andINFINITY
or introducestatic
methods that returnZERO
andINFINITY
. For example:Note that the
X
type-parameters are valid for the corresponding static method only and are not shared across the class.The problem, however, with this approach is that there's no way to ensure that the instance type-parameter (
T
) is the same as the static method's type-parameter (X
), which may cause serious problems when used incorrectly.Also note that
static
methods are allowed in interfaces in Java8, so if you're using an order Java version, you should convert theTimeInterface
to a class.From the javadoc directly:
We cannot declare static fields whose types are type parameters
A class's static field is a class-level variable shared by all non-static objects of the class. Hence, static fields of type parameters are not allowed. Consider the following class:
If static fields of type parameters were allowed, then the following code would be confused:
Because the static field os is shared by phone, pager, and pc, what is the actual type of os? It cannot be Smartphone, Pager, and
TabletPC
at the same time. You cannot, therefore, create static fields of type parameters.