NetBeans Quick Tip #2 - Generating Getters and Setters



Somebody has asked recently on of the mailling lists how to generate getters and setters in NetBeans. I've mentioned that some people even thought NetBeans doesn't have that functionality! So yes, it exists, only it's disguised as "Encapsulate Fields".

You can invoke the action either from the context menu in editor or from the main menu. In both cases use Refactor | Encapsulate Fields. The following dialog pops up and it lets you create getters and setters for all fields in the class:



The good news is that generating getters and setters will be available through the code completion in next release, which will make this feature much more discoverable:

Comments:

In the older versions of NB (at least 3.6) the "create getter and setter" had a functionnality that is now gone: generate javadoc for these getter and setter. Is there a tip that I missed with the "Encapsulate fields" refactoring tool?

Posted by Valère Déjardin on květen 26, 2005 at 02:07 dop. CEST #

I hope the "Create g/setter" in the code completion will only be available if those g/setters don't already exist.

Posted by Jamie Lawrence on květen 26, 2005 at 04:11 dop. CEST #

To Valere: I don't think there's such an option. You can add the javadoc through autocomment tool. But I understand you would like to have this automated. There's a filed enhancement for this in Issuezilla, feel free to vote for it.

To Jamie: yes and it works already this way in the experimental version from which I made the screenshot. Only non-existing getters/setters appear as options.

Posted by Roman Strobl on květen 26, 2005 at 04:31 dop. CEST #

Thanks, I voted for it.

Posted by Valère Déjardin on květen 26, 2005 at 10:21 dop. CEST #

It would also be nice if you could just annotate the field with @property and the IDE would create the getters for me. <p/> Iwan

Posted by Iwan Eising on září 02, 2005 at 12:23 dop. CEST #

Hi Roumen,
Thanks for the great tips. Here's a suggestion I haven't seen anywhere yet, it's regarding getters and setters: How about 'boxing' the Object-versions of the primitive types in getters and setters?
I quite often need to use these object primitves, and most commonly Boolean, because I need to work with objects rather than primitives, but it is so handy to treat them like primitives, so I create getters and setters that work with the primitives as well. For Boolean objects I create not only the getMyBoolean, but also the isMyBoolean which returns a <u>b</u>oolean. Thanks, Iwan

Posted by Iwan Eising on září 02, 2005 at 12:28 dop. CEST #

Hi Roumen,

Any chance of generating PropertyChange support for the setters as well?

Posted by Anonymous Person on říjen 25, 2007 at 02:27 odp. CEST #

Post a Comment:
Comments are closed for this entry.
About

Roman Strobl

Search

Archives
« duben 2014
PoÚtStČtSoNe
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today