Hi guys,
with event handlings it seems that there are 2 approaches:
1)using an anonymous inner class
2)using a normal inner class.
From what I can see if you have more than one component that is generating an event, say you have 3 buttons, if you go down the route of anonymous inner classes then you need to have effectively 3 inner classes, one for each component. If you instead use a normal - I call it normal, not sure what's the right name for it - inner class then you could techincally use only one.
So, what's better? My understanding is that anonymous inner classes are more widely used because there is less code to write, that's if you use 1, but that might not be the case. What do you guys say? I'm writing a little simple application with three buttons, and I'm thinking not to use an anonymous inner class.
thanks
Recommended Answers
Jump to PostThe AIC approach gives you a separate handler for each event, which is typically what you want. You use a "normal" inner class when you want to pass parameters into the handler when setting it up - that's something you do when you create dynamiclally changing layouts for example. Using …
Jump to Postsomething that might make a difference between an anonymous inner class, and a named one is, do you plan to use it for several components? if so, it would be crazy to re-write the anonymous inner classes each time you want to use it, if you can just re-use an …
Jump to PostAgreed, provided the reason for using for several components is that there is a lot of commonality between them (see example in previous post!). In beginners code we often see a single handler used for unrealeted actions, eg Save, Cancel, OK, Undo etc, so you get the horrible long switch …
All 12 Replies
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.