Skip to content

Instantly share code, notes, and snippets.

@evelyn-fyi
Last active November 23, 2025 20:02
Show Gist options
  • Select an option

  • Save evelyn-fyi/2dd608dd3ebf1365246550263e66fa8b to your computer and use it in GitHub Desktop.

Select an option

Save evelyn-fyi/2dd608dd3ebf1365246550263e66fa8b to your computer and use it in GitHub Desktop.
RAD Women II Static vs. Non Static Discussion File
//instance of a class
TestStaticVsNonStatic myTestClass = new TestStaticVsNonstatic('This String Is A Parameter');
//accessing class variables
System.debug('myStaticString: ' + TestStaticVsNonStatic.myStaticString); //we access via ClassName.VariableName
System.debug('myNonStaticString: ' + myTestClass.myNonStaticString); //we access via InstanceName.VariableName
//access instance of myStaticString
//commented out because we expect an error
//System.debug('myStaticString: ' + myTestClass.myStaticString); //we access via InstanceName.VariableName => get "static variable in non-static context error"
//access class variable of myNonStaticString
//commented out because we expect an error
//System.debug('myNonStaticString: ' + TestStaticVsNonStatic.myNonStaticString); //we access via ClassName.VariableName => get "variable does not exist error" because we have not created an instance of the class
//call static method
TestStaticVsNonStatic.myStaticMethod(); //dot notation ClassName.MethodName()
//call nonstatic method
myTestClass.myNonStaticMethod(); //dot notation InstanceName.MethodName();
public class TestStaticVsNonStatic {
//class variables
public static String myStaticString = 'My String Is Static'; //Accessible via dot notation ClassName.VariableName
public String myNonStaticString;
//constructor
public TestStaticVsNonStatic(String nonstaticString) {
this.myNonStaticString = nonstaticString; //This Instance Is Non-Static: access non-static/instance variables with `this.`
}
public static void myStaticMethod() {
System.Debug('Calling Static Method');
}
public void myNonStaticMethod() {
System.Debug('Calling Non-Static Method');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment