GWT - EventBus calling event for object that no longer exists -


i ran problem in large program, wrote small sample test it. i'm hoping can explain me.

here's code:

eventbus bus = new simpleeventbus();  class testclass {     testclass() {         bus.addhandler(testevent.type, new testhandler() {              @override             public void onevent(testevent event) {                 system.out.println("test");             }          });     } }  class testevent extends gwtevent<testhandler> {     public static final gwtevent.type<testhandler> type = new gwtevent.type<testhandler>();      @override     public type<testhandler> getassociatedtype() {         return type;     }      @override     protected void dispatch(testhandler handler) {         handler.onevent(this);     } }  interface testhandler extends eventhandler {     void onevent(testevent event); } 

after following:

testclass c1 = new testclass(); c1 = new testclass(); bus.fireevent(new testevent()); 

now logic, output should single "test"; in fact, 2 "test"s, handler called twice. don't why is; c1 single object, shouldn't handler called once?

you created 2 instances of testclass, referencing latter via c1. during each instantiation anonymous handler added single global bus. there 2 handlers bus has in collection , calls when fire testevent, hence 2 "test"s.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -