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
Prince_sfdcPrince_sfdc 

Aepx Static method execution

when I run the block of ‘static’ code in execute anonymous, it throws an error “only top-level methods in the class are allowed to be declared as static”. I tried my code also: there also same issue.
 public StringArrayTest {  public static void generateStringArray() {}  }
 Can anyone help on this?

thank you.
Best Answer chosen by Prince_sfdc
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
HI Prince,

When you define a class in an anonymous block, the class is considered virtual by default when the anonymous block executes. Save your class in Salesforce to avoid this from happening. 

All Answers

ManojjenaManojjena
Hi Prince,

Below code I am not able to understand .Could you please explain . 
public StringArrayTest {  public static void generateStringArray() {}  }

 
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Hi Prince, 

You cannot use static methods in your wrapper/inner class. In your case, create another class and copy the wrapper/innser class code to the new class. 



 
Prince_sfdcPrince_sfdc
Hi Thiyagarjan,
I've not created any wrapper/inner class. I just created a static method in a class and excuted in the anonymous and it threw error as given in my question..

Thanks for reply..!


 
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Can you post the whole code?
 
Prince_sfdcPrince_sfdc
Hi Manoj,

My bad, missed the class keyword in the question...
its like below: 
 public class StringArrayTest{
 public static void generateStringArray()
{
     system.debug('array to be printed..');
}
 }

created a static method in a class and excuted in the anonymous and it threw error as given in my question..


 
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
HI Prince,

When you define a class in an anonymous block, the class is considered virtual by default when the anonymous block executes. Save your class in Salesforce to avoid this from happening. 
This was selected as the best answer
Prince_sfdcPrince_sfdc
Hi Thiyagarajan,
Yes, you're absolutely right. Now I've gotten solution for this problem. 
And eventually, my observation is that we can run any snippet in the anonymous window, but if we are keeping class in the snippet then it should not contain a static method. In this particular code given in my question I've included a static method in a class that's not allowed. 
Had it been a case where I've used a code w/o static method like below it would have run: 
e.g. code I tried w/o static method in class it runs successfully.  
 
​public class prince{
    public void cris(){
        system.debug('prince_cris');
    }
}

prince pc = new prince();
pc.cris();

Thanks 
Thiyagarjan and Manoj


 
Prince_sfdcPrince_sfdc

@Thiyagarajan: And to add to your point that we can save the class and make instance and run in the excute anonymous.
But if we don't want to save the class we should not include static method in that.