How can there be existed more than one object at runtime even we have used singleton pattern?
with "Reflection" it can be done.
http://en.wikipedia.org/wiki/Reflection_%28computer_science%29
I think I guess correctly but you are the decider which is the correct one????
hik hik....
just google it and find the answer.
code for learn something
public class A {
private static A a = null;
//singleton pattern
public static A getInstance(){
if(a==null)
a = new A();
return a;
}
void m(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
A a = getInstance();
A.B b = a.new B();
a.m("m from A");
b.x(a);
}
class B{
void x(A a){
A t = A.getInstance();
t.m("x from B");
System.out.println("Both of objects are the same one? "+t.equals(a));
}
}
}
http://en.wikipedia.org/wiki/Reflection_%28computer_science%29
I think I guess correctly but you are the decider which is the correct one????
hik hik....
just google it and find the answer.
code for learn something
public class A {
private static A a = null;
//singleton pattern
public static A getInstance(){
if(a==null)
a = new A();
return a;
}
void m(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
A a = getInstance();
A.B b = a.new B();
a.m("m from A");
b.x(a);
}
class B{
void x(A a){
A t = A.getInstance();
t.m("x from B");
System.out.println("Both of objects are the same one? "+t.equals(a));
}
}
}
Comments
Post a Comment