While researching the possibility of using generics in apex I can across a blog with this:

"Generics are partially supported in Apex, you must be using them with Collections and Batch Apex."

However, I couldn't really find any more information on that. Can generics be used in apex? What are the limitations?

By generic I mean in a "generic programming" sense where types are not defined in code but determined at run time. For example, I would like to be able to write a List iterator in apex but not have to define the object type. Something like:

global class ListIterator implements Iterator<T>{
  global boolean hasNext(){ 

  global T next(){        

Where T would be defined at runtime - it could be Account, or Opportunity, or CustomObject1 or CustomClassX. Not only would it work with these data types but when I call hasNext() it would return an instance of that class (as opposed to returning a generic Object or sObject that I would then have to cast).

  • It's not clear to me what you mean by "generic". Can you give an example of what you're doing, or trying to do? Commented Sep 6, 2012 at 18:26
  • I added an example and clarification in an edit to the question. Commented Sep 6, 2012 at 18:39

2 Answers 2


NOTE: This answer is not useful for any code saved in v26.0 or later.

See Winter '13 Release Notes pg. 191 (193 of the PDF)

Take a look at the Parameterized Typing and Interfaces section in the Apex Developer's Guide. This is what you are looking for, I'm pretty sure.

The other way that Generics are commonly used in Apex are what you stated which is in the Collections and Batch Apex classes.

For example, without generics you'd have something like:

List contacts = new List();
contacts.add(new Contact());
contacts.add(new Account());  // uh oh.

With generics you have the compile time type safety:

List<Contact> contacts = new List<Contact>();
contacts.add(new Contact());
contacts.add(new Account()); // won't save/compile because the list knows it only has Contacts.

So, in theory there's some generic code somewhere in the Apex system that handles all of the List work such as:

public class List<T> {
    public void add(T element) { 
      // do the magic
    public T get(Integer index) {
      // do the magic
  • 9
    WARNING - From the Winter 13 release notes: "Parameterized interfaces are not supported in Apex saved using Salesforce.com API version 26.0 or later." My guess is that nobody has used them.
    – kibitzer
    Commented Sep 6, 2012 at 19:51
  • Huh? Are they replacing it with something else or just getting rid of it or what? Commented Sep 6, 2012 at 19:52
  • 2
    na1.salesforce.com/help/doc/en/… (p. 175) - Desupport of Parameterized Interfaces. I also figure that nobody used the parameterized interfaces. Commented Sep 6, 2012 at 19:56
  • 4
    that's quite a bummer actually. Hopefully generics will be returned to apex somehow
    – pjcarly
    Commented Nov 12, 2012 at 13:51

I guess we all need to vote here to make them hear us and allow generics in Apex. This is the only way how generics can appear in Apex. If no ones votes they think we don't really need them.

  • 5
    Seems like more of a comment than an answer.
    – Adrian Larson
    Commented Feb 7, 2017 at 12:05
  • 5
    No, this is actually answer to question. The question is: "Can generics be used in apex?" The answer is - "Not yet. However, if you vote up the corresponding appeal, you might use it in future". Since it is not obvious by looking through existing answer
    – Patlatus
    Commented Feb 7, 2017 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .