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
Louis SummersLouis Summers 

Trailhead challenge to Create an Apex class

In the introduction to Apex, I was surprised that the challenge was such a leap for an introductory lesson, but I never back away from a challenge.  I have attempted to create the class which requires much more knowledge than is presented.  The method is to be public and static.  But the complier message says that constructors cannot be static.  I see no reason why my method should be interpreted to be a constructor. Can anyone suggest why this is happening.  I'm a Newbie, so it could be a simple correction, but I have done some extensive research before asking for assistance.  Thanks in advance.

Task "Create an Apex class that returns an array (or list) of strings"
bob_buzzardbob_buzzard
The method would be interpreted as a constructor if you didn't specify a return type.  E.g. given an apex class named 'MyClass' with a method 'MyMethod':
 
public with sharing class MyClass
{
    public static MyMethod(String param1)
    {
        // logic here
    }
}

MyMethod would be interpreted as a constructor as it doesn't have a return type.  If the method is not intended to return a result, it should define the return type 'void':
 
public with sharing class MyClass
{
    public static void MyMethod(String param1)
    {
        // logic here
    }
}

 
Louis SummersLouis Summers
Hello Bob, This is my first attempt at Apex code. My error is indicated on line 4, and the error text is ³Constructors cannot be static². As the challenge indicates that the method must be static and I believe that I have what is a return type, I am confused as to why I am receiving this error. Again, this is my first attempt, so the problem may be a very simple ³beginners² error. Thanks. public class StringArrayTest { // Public method public static generateStringArray(Integer arrSize) { List testNum = new List(arrSize); for (Integer n=0;n
bob_buzzardbob_buzzard
Your method:
 
public static generateStringArray(Integer arrSize)

Doesn't define a return value, so the compiler assumes its a constructor.  As your method is going to return an array (or list) of strings, you need to declare that:
 
public static List<String> generateStringArray(Integer arrSize)