I created a program using java. now that Im done I build it. How can I make my program can run just once or what they called Single Instances.
Asked
Active
Viewed 140 times
1 Answers
-1
This is how you create a Singleton Class
public class MyClass
{
private MyClass instance;
private MyClass() { }
public static MyClass getInstance()
{
return instance == null ? new MyClass() : instance;
}
}
So where-ever you are trying to invoke the class do it this way
public class Main
{
public static void main(String[] args)
{
MyClass mc = MyClass.getInstance();
//With this only one instance of your class is possible
}
}

asifsid88
- 4,631
- 20
- 30
-
4The OP is asking how to have a single instance of the program (i.e. one JVM), not how to create a singleton class. – Cameron Skinner Feb 21 '13 at 06:49