6

I have something like the following in an Apex class:

public class Picker 
{
    public static List<Type__c> getTypes()
    {
        List<Type__c> list = [select Name from Type__c where Name = 'Person'];
        return list;
    }
}

Which gives the following error:

Error: Compile Error: unexpected token: 'List' at line 5 column 8

However, turning into the following is fine:

public class Picker
{
    public static List<Type__c> getTypes()
    {
        return [select Name from Type__c where Name = 'Person'];
    }
}

Why does the first code segment not work?

2 Answers 2

5

"List" is a reserved keyword and hence it cant be used as a variable name .You can change the name and should work

public class Picker{
   public static List<Type__c> getTypes(){
      List<Type__c> listtype= [select Name from Type__c where Name = 'Person'];
     return listtype;
  }
}
2
  • 3
    Interesting, it didn't even occur to me that keywords were case insensitive. Thanks. Dec 12, 2014 at 18:54
  • The instructor kind of harped on it and I still made that very same mistake. I guess it's ingrained from Java... Dec 28, 2021 at 22:35
1

Change your variable name from list to listTypes and that should fix it up.

For reference see this link here:

Naming Variables, Methods and Classes

You cannot use any of the Apex reserved keywords when naming variables, methods or classes. These include words that are part of Apex and the Force.com platform, such as list, test, or account, as well as reserved keywords.

You must log in to answer this question.

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