Okay to construct object in static constructor?

  • Thread starter Thread starter Adam Smith
  • Start date Start date
A

Adam Smith

Hello. I'm wondering if it's okay to construct the same object within
static constructor:

public class Proc
{
public Dataholder d;//some other class
//I've created
static ArrayList al;

int val;

static Proc
{
al = new ArrayList();
for(int n= 0; n<5; n++)
al.Add(new Proc()); //This is the line I'm concerned
//about, adding a new object of the
//same type within static constructor
}

public proc()
{
val = 3;
}
}

Thanks in advance!

Adam Smith
 
Adam Smith said:
Hello. I'm wondering if it's okay to construct the same object within
static constructor:

Yes, and indeed it's often used when implementing the singleton
pattern.
 
Back
Top