+ Start a Discussion
Sales Force FRMSales Force FRM 

Unable to make use of List as it's generating compile time issues

    Hi Folks,
                   I am  Unable to make use of List as it's generating  compile issues , ,y program is as follows:

1 public class ListDemo
2 {
3   List<Integer> MyList = new List<Integer>(); // Define a new list
4    MyList.add(47); // Adds a second element of value 47 to the end
5   List.get(0); // Retrieves the element at index 0
6    MyList.set(0, 1); // Adds the integer 1 to the list at index 0
7   MyList.clear(); // Removes all elements from the list
9 }

and i got following  compile time errors:
Error: Compile Error: unexpected token: 47 at line 4 column 16

iam running apex scripts from the  from the following links:

I have  a developer edition account ,
I have tried so many code samples (Please refer code samples in the above link)
but all most all i got compile time errors like above!,  please suggest me what  i have to do?


I can replicated this as well. It appears to only happen with new classes. My older classes that have similar code seem to save fine.

public class listTest{

List<Integer> myList = new List<Integer>();


Error: Compile Error: unexpected token: myList.add at line 4 column 1


Message Edited by TehNrd on 05-21-2008 09:28 AM
The error message is very misleading, but the actual problem is that you can't have code in a class, it has to be in a method. e.g.

public class listTest{

public void intList {
List myList = new List();

What about visualforce controllers? I definitely have code in the class and they work fine.

Edit: Duh, I have variables defined in the class but no code. I should have caught that.

Message Edited by TehNrd on 05-21-2008 11:31 AM