Declare static functions in anonymous Apex
Declare static functions and classes in Anonymous Apex
Declare static functions and classes in Anonymous Apex
Did you know that the code written in Anonymous Apex is compiled in class Anon? This means that you can use static functions and classes in Anonymous Apex without even deploying them to the org:
apex
public class MyClass {
public MyClass() {
System.debug('My Class');
}
}
Anon.MyClass t = new Anon.MyClass();MyClass is an inner class so it can’t have static properties and methods:
Don't do this
public class MyClass {
public MyClass() {
System.debug('My Class');
}
static String myStaticMethod() {
System.debug('This is not going to work!');
}
}If you need to use static methods you can declare them directly in Anonymous Apex:
apex
public static void myStaticMethod() {
System.debug('My Static Method');
}
Anon.myStaticMethod();

