I have some code which is time-consuming, you can take it as loading data from disk (but definitely not only that). But I only need this piece of code once in initialization, but it finish, I can use some method related to it directly and quickly.
Now I have no idea where should I put this piece of code, here is my idea: I put it in the constructor in some class A, then in class B, which needs to call some method in A a lot of times, so I make a new instance of A in B's constructor, which is very ideal because each time I need A's method, I can directly access it and A's constructor is only called once.
But here is the problem, I have some other class C, which generate a lot of B instances (new B()), then each time C generates a new instance of B, A's constructor will be called, it is again very time consuming, how can I organize my code to avoid that?
See code for clarification.
class A{
    public A(){
        some time-consuming code; 
    }
    public void methodInA(){
        some method in A;
    }
}
class B{
    public B(){
        A a = new A();
        for (i=0; i<10000;i++)
            methodInB();
    }
    public void methodInB(){
        methodInA();
    }
}
//so far, everything is perfect, but I have another class C 
class C{
    public C(){
        some code;
    }
    public void methodInC(){
        for (i = 0; i<10000; i++)
            new B();
    }
}
 
     
     
     
    