Reputation: 2305
I created a class with 6 private static methods and a private constructor. The private constructor runs all of the static methods. I want to call the class's private constructor in another class, but I'm not able to. All I want is to run this class once without creating an instance of anything. The class populates a small database and I have no need for it other than calling it once.
I could put it into a method, but I don't want to put unrelated code into my main class. I want everything more separated. I could just do it with a public constructor and create an instance of the class, but I don't see why I would have to do it that way when an instance isn't needed.
Is there a good way to accomplish what I'm trying to do?
Upvotes: 0
Views: 2513
Reputation: 2272
If you want to call it or its members from another class, you will need to make a public method.
If you don't want any instances of this class around, then you should make a public static method that can be called.
The public static method should have a static boolean. When called, it checks that value, and if not set, switches the static boolean value (so it knows it has been called before) and then calls all the private static methods that need to be run once and only once.
If you only need this initialization done once ever to the database, rather than once per effort, then you should have your static code run out and check the database to see if it has already been initialized. You could make this particularly easy by having a one row table that holds a boolean in it that you can test. Just update the initialization code to set that value when the DB is initialized, and have your start up code test for that value to determine its value and what actions to take based upon that value.
Upvotes: 0
Reputation: 5779
Why not replace your private constructor with a public static method?
Your original code:
public class DatabaseInitializer
{
private DatabaseInitializer()
{
init1();
init2();
...
}
private static void init1() { ... }
private static void init2() { ... }
...
}
Your new code
public class DatabaseInitializer
{
public static void Init()
{
init1();
init2();
...
}
private static void init1() { ... }
private static void init2() { ... }
...
}
Than you call it:
Main()
{
DatabaseInitializer.Init();
}
Upvotes: 2
Reputation: 1388
I want to call the class's private constructor in another class
--
All I want is to run this class once without creating an instance of anything
If you do not want to create an instance of your class, then do not use the constructor. I think you just want to use a class to "separate" some code? Use a static method for that.
Or if this code should run once and call some static methods. You can use a static ctor
class B
{
static B() {
//this stuff called when you create this class or when a static member is referenced
}
Upvotes: 1
Reputation: 1272
Singleton would make exact one Instance. If you don't want an Instance, just make one static method public. If you want to make sure this is called only once make a static counter or a bool in your class which stop the method from being called a second time call constructor without an instance is impossible, even if it was public
Upvotes: 2
Reputation: 5600
Upvotes: 0