Search code examples
javaapex-code

How to close or destroy a class after calling it?


I have a call to a method in a class as follows:

String key1 = "test1";
String key2 = "test2";

long[] datalr = new long[] {};

TestClass myTC1 = new TestClass(); 
datalr = myTC1.callerSub(key1,key2);
TestClass myTC2 = new TestClass(); 
datalr = myTC2.callerSub(key1,key2);

The test class is as follows:

public class TestClass {

  private final Long[] P = new Long[18];



  Public TestClass {
  }

  public long[] callerSub(string key1, String key2){

    long[] datalr = new long[] {0,0};
    integer i;
    system.debug(LoggingLevel.INFO,P[0]);

    datalr[0] = 2;
    datalr[1] = 3;
    for ( i = 0; i < 18; ++i ){
      P[i] = i+1
    }
    return datalr;

  }
}

I am calling the TestClass.callerSub twice, The first time I call the debug statement:

system.debug(LoggingLevel.INFO,P[0]);

it returns null, as it should,

The second time I call it, the P[i] now has a value,

How do I close or destroy the TestClass class before I call it again?

Thanks


Solution

  • I guess your method is static?

    You can make your P a variable, and not a static field anymore, or make your method an instance method and your field P an instance field (by removing the static keyword). Then you can create a new instance for each call: new TestClass().callerSub(key1, key2)