Linked Questions

0 votes
3 answers
572 views

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 ...
Java Impatient's user avatar
2 votes
2 answers
898 views

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 ...
Nathan's user avatar
  • 65
2 votes
1 answer
684 views

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 ...
user308485's user avatar
0 votes
0 answers
949 views

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<...
Alpha's user avatar
  • 14.2k
1 vote
4 answers
443 views

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 ...
Sandeep's user avatar
  • 1,431
0 votes
1 answer
411 views

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 ...
Rohr Facu's user avatar
  • 737
-2 votes
1 answer
89 views

As the title states, why can't Java generics be used for static methods?
Jswq's user avatar
  • 786
26 votes
2 answers
10k views

public static BTNode<E> treeCopy(BTNode<E> source) { if(source == null) return null; else { BTNode left = BTNode.treeCopy(source.left); BTNode ...
Set of Theseus's user avatar
13 votes
3 answers
8k views

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(); ...
Leon's user avatar
  • 438
1 vote
3 answers
6k views

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....
Adam Lee's user avatar
  • 26k
5 votes
2 answers
4k views

@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 ...
Dean J's user avatar
  • 40.5k
1 vote
2 answers
11k views

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(...
masterdany88's user avatar
  • 5,369
3 votes
5 answers
3k views

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(...
javanoob's user avatar
  • 6,462
3 votes
3 answers
1k views

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 ...
user826323's user avatar
  • 2,350
1 vote
1 answer
3k views

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 ...
Suparna's user avatar
  • 1,192

15 30 50 per page