I believe this is a basic query and am missing something out. However am exasperated finding an answer to the issue and thus seek your guidance.
The intent is to create a static method generateStringArray() that can return an array. However whenever I run the code in the below form, it throws error:
Static method cannot be referenced from a non static context:
List<String>
- My method is static and arrays can be returned from static
- Instead of string, if I use void and don't return, the compiler does run
Why is this throwing an error? What part of the code is being run as a class instance and not static? Does it have soemthing to do with the way I have defined the function or the string ?
I have checked at other solutions available for the similar issue but my query pertains to what is not static in the code?
Code
public class test_clonestring {
public static String[] generateStringArray (Integer lenstr){
String[] sstring = new String[lenstr];
for (Integer i = 0; i<lenstr; i++) {
sstring[i] = 'Test ' + i;
}
system.debug('STD_DBG:0:'+sstring);
return (sstring);
}
}
test_clonestring.generateStringArray(2)
and this runs without an error-test_clonestring obj = new test_clonestring();
/* String[] sarray = */ obj.generateStringArray(5);
Alternate2test_clonestring obj = new test_clonestring();
String[] sarray = obj.generateStringArray(5);