public class Solution {
public static void main(String[] args) throws Exception {
SimpleObject<String> stringObject = new StringObject<Object>();
}
interface SimpleObject<T> {
SimpleObject<T> getInstance();
}
//thrown off by the <T> what does this mean??
//and how am i supposed to create a class with an interface which has parameters?
}
Not sure what I am supposed to do here. confused by the T symbol
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Noam G
4 October 2019, 19:17
0
Guadalupe Gagnon
10 April 2019, 15:42useful
T is a placeholder for Class. This means that T will equal whatever class is passed to it. Think of it like method parameter like
public int sum(int x) <-- here x will equal whatever is passed to it in the calling code.
If you have used ArrayLists yet then you have used this format already. The ArrayList code is:
public class ArrayList<T>{}
So you can make ArrayList<String> or ArrayList<Integer> etc.
It is a little more involved than my explanation, but that should suffice. Here is a thread discussing it on stackoverflow:
https://stackoverflow.com/questions/6008241/what-is-the-difference-between-e-t-and-for-java-generics/6008274#6008274
+8