Singelton
Here is an example of a singelton class. Note the private constructor.
---------------------------------------------------------
import java.io.*;
class Singelton{
public int myInt;
private static Singelton me = new Singelton();
private Singelton(){
}
public static Singelton getInstance(){
return me;
}
}
---------------------------------------------------------
Here is a wrapper class that demonstrats that all instances of
the singelton class are really shared instances of one.
(when all is one and one is all, --wow man thats deep)
---------------------------------------------------------
import java.io.*;
class Wrapper{
public static void main(String[] args){
System.out.println("class: Wrapper, method: main");
Singelton one = Singelton.getInstance();
Singelton two = Singelton.getInstance();
Singelton three = Singelton.getInstance();
if(one == null)
System.out.println("oops");
one.myInt = 7; //yea yea I am not going to make accessors for this example.
System.out.println("changed one and two is: "+two.myInt );
System.out.println("changed one and three is: "+three.myInt );
}
}
-----------------------------------------------------------
here is the tar with these two java files in it.
Singelton