I have a Java class that looks like this:
public class My_ABC
{
int a=0;
boolean B=true;
static // Initialize and load existing data only once at start-up
{
// need to know if it's called from its own main()
// or by another class to conditionally set veriables
}
public My_ABC(int AA,boolean BB)
{
}
public static void main(String[] args)
{
My_ABC my_abc = new My_ABC(3,true);
}
}
Because the static block is run when the class is loaded, how can I detect if it's called from it's own main()
or by another class to conditionally set variables?
I understand when some of you said "All sorts of bells go off!" Well, it's because I got a situation: I'm designing a class that needs to load lots of data to the limit of my PC (4G Ram), and I'm running 32-bit version of Java which can only use 1.5G of Ram max; so when I test this class by itself, I need to load as much data as possible to test all possible situations, but when it is called from multiple other classes, it can't do that (would cause out of heap space error), so it should only load min. data needed. And yet since all the data should only be loaded once at start up, it should be in the static block; otherwise I need to implement extra logic to detect if it's being loaded the first time (need to load data), or 2nd, 3rd time (shouldn't load data again and again). And if I implement extra logic to do that and move the data load code out of the static block, it would cause unnecessary complexity because if I move to 64-bit version of java (hopefully soon), that extra complexity would be extra burden (I'll have enough space to load all data even when being called from other classes). So the temp quick fix is to detect it in the static block and handle the difference accordingly, when I have enough space, just comment them out without the need to change programming logic structure.
Thanks for all the answers and advices, I tried the "StackTraceElement" approach, it works great! It solved my problem.