Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using generic types in a static context

public static BTNode<E> treeCopy(BTNode<E> source)
{
     if(source == null)
         return null;
     else
     {
         BTNode left = BTNode.treeCopy(source.left);
         BTNode right = BTNode.treeCopy(source.right);
         return new BTNode(source.data, left, right);
     }

}

My questions is why can't I use the generic Type E in a static context? I tried search for several answers couldn't find any that make snese.

like image 832
Ethan Avatar asked Nov 17 '10 20:11

Ethan


People also ask

Can you use generics in static method?

1 Answer. You can't use a class's generic type parameters in static methods or static fields. The class's type parameters are only in scope for instance methods and instance fields.

Can a static class be generic?

A static generic class is exactly as useful as any given static class. The difference is that you don't have to use copy-and-paste to create a version of the static class for each type you want it to work on. You make the class generic, and you can "generate" one version for each set of type parameters.

Why do we use generic data types?

Generics enable the use of stronger type-checking, the elimination of casts, and the ability to develop generic algorithms. Without generics, many of the features that we use in Java today would not be possible.

Which types can be used as arguments of a generic type?

The actual type arguments of a generic type are. reference types, wildcards, or. parameterized types (i.e. instantiations of other generic types).


2 Answers

You need to declare the generic type in the method signature. Since this is a static method - it cannot grab generic information from anywhere else. So it needs to be declared right here:

public static <E> BTNode<E> treeCopy(BTNode<E> source)
{
     if(source == null)
         return null;
     else
     {
         BTNode<E> left = BTNode.treeCopy(source.left);
         BTNode<E> right = BTNode.treeCopy(source.right);
         return new BTNode(source.data, left, right);
     }
}
like image 143
jjnguy Avatar answered Oct 01 '22 13:10

jjnguy


E can mean anything. To use E (as a generic) you need to create an instance of an object. You cannot do this if you have a static method because the generic type-parameters are only in scope for an instance of the class (which includes its instance methods and instance fields).

Static members and fields belong to every instance of the class. So if you had an instance of BTNode<String> and another instance of BTNode<Integer>, what exactly should the static treeCopy be using? String or Integer?

There is a workaroud; you have to tell the static method what E means. So you will have to define it like this:

public static <E> BTNode<E> treeCopy(BTNode<E> source)

It would also help to take a second look at your design and see if this is actually what you want.

like image 28
Vivin Paliath Avatar answered Oct 01 '22 13:10

Vivin Paliath