Okay to construct object in static constructor?

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
 
J

Jon Skeet

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top