Thursday, June 9, 2011

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));
}
}

}

No comments:

Post a Comment

Let's try to build scrum masters/project managers/software architects/even a company with training AI models

The concept: The basic concept is to build trained AI model for each role separately in scalable fashion within a private cloud. As an examp...