Skip to main content

I was doing a challenge in which I have to return a list of string (formatted in a way) as per the integer specified in the function, it works when static declaration is not given in function, but not when static declaration is there. 

 

It compies succesfully, but when executing it in Anon window, 

this error is coming. 

 

"Line: 2, Column: 4 Static method cannot be referenced from a non static context: List<String> StringArrayTest.generateStringArray(Integer)"

 

Below code. 

public class StringArrayTest {

public static List<String> generateStringArray(Integer size){

List<String> values = new List<String>();

for(Integer i=0; i<size; i++){

values.add('Test '+String.valueOf(i));

}

return values;

}

}

3 respuestas
  1. Jeff Weller (PARQA Inc.) Forum Ambassador
    29 sept 2022, 06:04

    Hello @Vivaan Shiromani,

    please refer 

    Hello ,please refer public class StringArrayTest {public static List<String> generateStringArray(integer length){ List<string> strArr = new List<string>(); for(Integer i=0; i<length; i++) { strArr.

     

    public class StringArrayTest {

    public static List<String> generateStringArray(integer length)

    {

    List<string> strArr = new List<string>();

    for(Integer i=0; i<length; i++)

    {

    strArr.add('Test ' +i);

    System.debug (strArr[i]);

    }

    return strArr;

    }

    }

0/9000