-2

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
kelvzy
  • 913
  • 2
  • 12
  • 19
  • 4
    See [how-to-implement-a-single-instance-java-application](http://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application) – Anujith Feb 21 '13 at 06:24

1 Answers1

-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