function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brian OconnellBrian Oconnell 

Constructors cannot be static

Doing Trailhead exercise here: https://developer.salesforce.com/en/trailhead/force_com_programmatic_beginner/apex_database/apex_database_intro

This is my code:
 
public class StringArrayTest {
    public static generateStringArray(Integer x) {
        List<String> stringList = new List<String>();
        for (integer n=0; n<x; n++) {
            stringList.add('Test '+n);
        }
        return stringList;
    }
}
I get the error message "Constructors cannot be static" on line 2, but I can not find anywhere an example of a public static method that returns a value.  How can I fix this?

Thank you,
Brian
 
Best Answer chosen by Brian Oconnell
Roy LuoRoy Luo
public class StringArrayTest {
    public static List<String> generateStringArray(Integer x) {
        List<String> stringList = new List<String>();
        for (integer n=0; n<x; n++) {
            stringList.add('Test '+n);
        }
        return stringList;
    }
}

All Answers

Roy LuoRoy Luo
public class StringArrayTest {
    public static List<String> generateStringArray(Integer x) {
        List<String> stringList = new List<String>();
        for (integer n=0; n<x; n++) {
            stringList.add('Test '+n);
        }
        return stringList;
    }
}
This was selected as the best answer
Brian OconnellBrian Oconnell
That's the syntax I couldn't find!  Thank you!
Brian OconnellBrian Oconnell
I clicked "Best Answer" but it's not working.
Roy LuoRoy Luo
What's the error? The return type was missing, just that simple.
 
public class StringArrayTest
 { 
     public static List<String> generateStringArray(Integer x) 
     { 
         List<String> stringList = new List<String>(); 
         for (integer n=0; n<x; n++) { stringList.add('Test '+n); } 
         return stringList;
     } 
}

I surely hope you didn't use '<b>List<String></b>' as the return type.