Singleton in same package same class,
package com.singletone.test;
class SingletoneBorn{
private static SingletoneBorn instanse = new SingletoneBorn();
private SingletoneBorn(){}
public static SingletoneBorn getInstanse() {
return instanse;
}
public void showDetails(){
System.out.println("This is singletone born world");
}
}
public class SingletoneTest {
public static void main(String args[]){
SingletoneBorn sb = SingletoneBorn.getInstanse();
sb.showDetails();
}
}
Singleton same package different class
Singleton class
package com.singletone.test;
public class SingletoneTest {
private static SingletoneTest instanse = new SingletoneTest();
private SingletoneTest(){}
public static SingletoneTest getInstanse() {
return instanse;
}
public void showDetails(){
System.out.println("This is singletone born world");
}
}
Singleton usage class
package com.singletone.test;
public class TestSingletone {
public static void main(String args[]){
SingletoneTest st = SingletoneTest.getInstanse();
st.showDetails();
}
}
Singleton different package different class
Singleton class
package com.singletone.test;
public class SingletoneTest {
private static SingletoneTest instanse = new SingletoneTest();
private SingletoneTest(){}
public static SingletoneTest getInstanse() {
return instanse;
}
public void showDetails(){
System.out.println("This is singletone born world");
}
}
Singleton usage class
package com.singletone.test2;
import com.singletone.test.SingletoneTest;
public class SingleTest {
public static void main(String args[]){
SingletoneTest st = SingletoneTest.getInstanse();
System.out.println("Singletone method call in nother package");
st.showDetails();
}
}