Linked Questions
31 questions linked to/from Static method in a generic class?
0 votes
3 answers
572 views
Generics - Why are Class type variables not valid in static contexts? [duplicate]
I am learning Java generics from a book. The book says that "Class Type Variables Are Not Valid in Static Contexts" and explains it with the following example. Consider a generic class with type ...
2 votes
2 answers
898 views
Accessing a static method of type T from within a generic class [duplicate]
I'm new to generics, and am struggling with applying to my situation: I currently have several SyncData Classes, one for each object type that I wish to sync. I'm looking at changing this to a single ...
2 votes
1 answer
684 views
Testing the main method of a generic class from another class [duplicate]
Someone wrote a generic class that I wish to test. It looks something like: public class < E > foo{ public static void main(String[] args){ do_stuff(); } } I have another ...
0 votes
0 answers
949 views
How to have method with dynamic type parameters using generics in java? [duplicate]
I'm trying to write a method(using generics) which takes two parameters and swap them but parameters can be of any type - int, char, float, String etc. Following is my code - public class SwapTest<...
1 vote
4 answers
443 views
Executing a generic Java class [duplicate]
My understanding is that a generic Java class needs to be parameterized over types before it can be put to use. I am surprised that the following sample code, in which the generic class has not been ...
0 votes
1 answer
411 views
Using static field in generic singleton [duplicate]
Thank you all for reading, i'm trying to understand Generics, and i got this excersice where i create a singleton with a generic parameter. public class Singleton<T> { public static T ...
-2 votes
1 answer
89 views
Why can't Java generic be used for static methods? [duplicate]
As the title states, why can't Java generics be used for static methods?
26 votes
2 answers
10k views
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 ...
13 votes
3 answers
8k views
Static Method in Interface with Generic signature
As of Java 8 you can have default or static methods implemented in Interfaces as the below public interface DbValuesEnumIface<ID, T extends Enum<T>> { T fromId(ID id); ID getId(); ...
1 vote
3 answers
6k views
How to call static method from a generic class?
I have a class containing a static create method. public class TestClass { public static <E> TestClass<E> create() { return new TestClass<E>(); } } when I use TestClass....
5 votes
2 answers
4k views
Are static members of a generic class different for different types in Java?
@Spence asked this Previous Question. So, how's that work in Java? Generic types are discarded at runtime in Java, so what happens to static variables of classes instantiated with different ...
1 vote
2 answers
11k views
Implement Generic Abstract Entity class with dao interface and implemantation
Hey I would like to have generic model/entity class that would download by id of type long element from db. The method for that is like this: public class GenericModel { @Id @GeneratedValue(...
3 votes
5 answers
3k views
Why we have to mention generic type parameter before the method return type?
Here on this page, it has the below code example to introduce the generic methods? public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) { return p1.getKey().equals(...
3 votes
3 answers
1k views
java static class Singleton with generic
I am trying to make a singleton like the following, but I keep getting a warning. If possible, I don't want suppress warning. Is there a way to do it? For now, I don't want to think about the ...
1 vote
1 answer
3k views
Java Generics - How to write static methods with type arguments?
Coming from a C++ background, I was hoping to write few util static methods which could be used throughout my application without necessarily going via object creation. And I was hoping to use the ...