My Assistant
![]() ![]() |
|
|
Jan 19 2011, 01:40 PM
Post
#1
|
|
|
UtterAccess VIP Posts: 7,132 From: Perris, California |
Does anyone know of any concerns that should prevent someone from wanting to use a double brace idiom as syntactic sugar?
An Example: CODE final gui.btn btnProc = new gui().new btn(pane) {{ setGridLoc(2,9); setCaption("Test"); }}; This would create a new btn Class (JButton), add it to the pane, and set the location and button text. This would be as opposed to the following code: CODE final gui.btn btnProc = new gui().new btn(pane); btnProc.setGridLoc(2,8); btnProc.setCaption("Process Copy"); The example provided is a bit remedial, but when I have multiple attributes of the class to set, it gets to be vertically longer than I would prefer, instead of being able to use up some of the horizontal space. And I'm also eliminating the need to specify which class object, which does avoid any conflict in case I mix up names (which happens). Thoughts? |
|
|
|
Jan 19 2011, 02:50 PM
Post
#2
|
|
|
UtterAccess VIP Posts: 1,215 From: Arcadia, California, USA |
Does anyone know of any concerns that should prevent someone from wanting to use a double brace idiom as syntactic sugar? I believe that this syntax is referred to as 'anonymous code block'. And I believe that it executes right after the constructor code blocks complete. In your case you are implicitly extending your gui.btn class with this anonymous code block.Thoughts? Precedents for this technique are seen a lot in example of adding new ActionListener { ... } or new Runnable { ... }. But in these cases we are simply implementing the abstract methods defined by these interfaces/abstract classes in an anonymous class code block.There may be reasons for not using this feature, but it is debatable. |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 24th May 2013 - 04:48 PM |