Hello everyone,
I have a question regarding multiple instantiations of a class and memory management. To illustrate my problem I have provided an example with two classes: Some_Class and Some_Other_Class. The constructor of some class uses a float and an integer as arguments and has a function which computes a certain number based on Var_A, Var_B and some formula. One of the methods in the class "Some_Other_Class" is used to store the different results of the method "Some_Function" in a vector of size "some_size". If I am to write the procedure the way I have shown below, an instance of the class "Some_Class" will be created "some_size" times in the method "Some_Routine". I know Java has automatic garbage collection, which brings me to the question: will Java dispose of the class instance after the end of each iteration or will all the instances of the class "Some_Class" be stored in memory.
************************************************** ***********************************
Thank you in advancepublic class Some_Class { private float Var_A; private int Var_B; public Some_Class(float A, int B){ Var_A = A; Var_B = B; } public float Some_Function(){ return Var_A * Var_B * Some_Complex_Formula; } } public class Some_Other_Class { . . . private Vector Some_Routine(float alpha){ Vector V = new Vector(some_size); for (int i = 0; i < some_size; i++) { SomeClass Ref = new Some_Class(i * some_float/alpha, i); V.elementAt(i) = Ref.Some_Function(); } return V; } } ************************************************************************************ Will it be more efficient (lower impact on memory) to use an argument free constructor for the class "Some_Class" and use the arguments in the method declaration. See snippet below: Alternative Way: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public class Some_Class { public Some_Class(){ // empty constructor } public float Some_Function(float Var_A, int Var_B){ return Var_A * Var_B * Some_Complex_Formula; } } public class Some_Other_Class { . . . private Vector Some_Routine(float alpha){ Vector V = new Vector(some_size); SomeClass Ref = new Some_Class(); for (int i = 0; i < some_size; i++) { V.elementAt(i) = Ref.Some_Function(i * some_float/alpha, i); } return V; } }