4

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?

4

"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;
  }
}
1
  • Interesting, it didn't even occur to me that keywords were case insensitive. Thanks. Dec 12 '14 at 18:54
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.