Is there a shortcut in Android Studio for automatically generating the getters and setters in a given class?
16 Answers
Using Alt+ Insert for Windows or Command+ N for Mac in the editor, you may easily generate getter and setter methods for any fields of your class. This has the same effect as using the Menu Bar -> Code -> Generate...
and then using shift or control button, select all the variables you need to add getters and setters
-
83For those on Macs it's control + enter. – anita Feb 26 '15 at 04:18
-
any possibility to create getter and setter such as ivar _username and getter and setter are getUsername and setUsername? – Nicolas Manzini May 04 '15 at 15:44
-
On Mac Control+n – Alon Kogan Feb 27 '18 at 11:33
-
2For java its working fine.But im using kotlin in android studio for app development.Then how to generate getter/setter like java? – SIVAKUMAR.J May 28 '18 at 07:59
-
3how to generate getters and setters for Kotlin class – Ashik Azeez Feb 18 '19 at 09:57
-
Command + N will not work on mac, use instead Control + N or Control + Enter – Muhammad Afzal Jun 08 '22 at 08:08
for macOS, ⌘+N by default.
Right-click and choose "Generate..." to see current mapping. You can select multiple fields for which to generate getters/setters with one step.
See http://www.jetbrains.com/idea/webhelp/generating-getters-and-setters.html

- 4,566
- 27
- 33

- 5,401
- 1
- 20
- 15
Android Studio & OSx :
Press cmd+n > Generate > Getter and Setter
Android Studio & Windows :
Press Alt + Insert > Generate > Getter and Setter
-
Just tested on Windows Alt+Shift+S+R is not working and right click does not contain Source. Is it maybe a custom setup? – Anthea Feb 09 '16 at 16:29
-

- 1
- 1

- 8,463
- 2
- 36
- 37
-
Any shortcut for **kotlin Constructors**? Alt + Ins not showing me generate Constructors option. – Arbaz.in May 02 '20 at 09:09
-
-
for that case, I don't know what's going on. Did you post your problem in the Intellij forum? – Ângelo Polotto May 06 '20 at 12:28
-

- 4,696
- 4
- 22
- 36

- 109
- 1
- 2
-
1Your image hasn't appeared in the post correctly, and you have not completed an image description - it says 'enter image description here'. – LordWilmore Aug 09 '16 at 15:45
You can generate getter and setter by following steps:
- Declare variables first.
- click on ALT+Insert on keyboard placing cursor down to variable declaration part
- now select constructor and press Ctrl+A on keyboard and click on Enter to create constructor.
- Now again placing cursor at next line of constructor closing brace , click ALT+INSERT and select getter and setter and again press CTRL+A to select all variables and hit Enter.
That's it. Happy coding!!

- 35,493
- 19
- 190
- 259

- 2,708
- 2
- 16
- 32
Position the cursor under the variables -> right-click -> Generate -> Getter and Setter -> Choose the variables to make the get and set
or
Alt + Insert -> Getter and Setter -> Choose the variables

- 326
- 4
- 16
-
The question was if it is possible to generate getters and setters with a single shortcut, so not where you can find this option in a menu. – R Pelzer Jan 04 '19 at 14:29
-
I did not understand. Alt + Insert -> Getter and Setter -> Choose the variables – Baby Mar 18 '19 at 14:45
As noted here, you can also customise the getter/setter generation to take prefixes and suffixes (e.g. m for instance variables) into account. Go to File->Settings
and expand Code Style
, select Java
, and add your prefixes/suffixes under the Code Generation
tab.

- 423
- 5
- 11
Using Alt+ Insert or Right-click and choose "Generate..." You may easily generate getter and setter or Override methods in Android Studio. This has the same effect as using the Menu Bar Code -> Generate...

- 932
- 7
- 14
This answer deals with your question but is not exactly an answer to it. =) It's an interesting library I found out recently and I want to share with you.
Project Lombok can generate common methods, such as getters, setters, equals()
and hashCode()
, toString()
, for your classes automatically. It replaces them with annotations reducing boilerplate code. To see a good example of code written using Lombok watch a video on the main page or read this article.
Android development with Lombok is easy and won't make your android application any 'heavier' because Lombok is a compile-time only library. It is important to configure your Android project properly.
Another example:
import lombok.Getter;
import lombok.Setter;
public class Profile {
@Getter @Setter
private String username;
@Getter @Setter
private String password;
}
Android development with Lombok is possible. Lombok should be a compile-time only dependency, as otherwise the entirety of Lombok will end up in your DEX files, wasting precious space. Gradle snippet:
dependencies {
compileOnly "org.projectlombok:lombok:1.16.18"
}
In addition you may want to add the Lombok IntelliJ plugin to support Lombok features in your IDE at development time. Also there is Hrisey library which is based on Lombok. Simply put, it's Lombok + Parcellable support.

- 35,493
- 19
- 190
- 259
-
1Unfortunately, Project Lombok breaks in Android library projects. It is unreliable right now. – IgorGanapolsky Feb 04 '15 at 00:33
-
1
-
1@IgorGanapolsky Project Lombok works fine with Android. See [setup guide](https://projectlombok.org/setup/android). – naXa stands with Ukraine Oct 25 '17 at 13:43
You can use AndroidAccessors
Plugin of Android Studio
to generate getter and setter without m as prefix to methods
Ex: mId;
Will generate getId()
and setId()
instead of getmId()
and setmId()

- 12,349
- 7
- 36
- 57
use code=>generate=>getter() and setter() dialog ,select all the variables ,generate all the getter(),setter() methods at one time.

- 919
- 12
- 13
Another funny way
Type the parameter name anywhere in the object after definition, you will see setter and getter, Just select and click enter :)
I tried with Android Studio 2.3

- 583
- 6
- 8
Right click on Editor
then Select Source -> Generate Getters and Setters
or press Alt
+ Shift
+ S

- 5,336
- 7
- 36
- 59
Just in case someone is working with Eclipse
Windows 8.1 OS | Eclipse Idle Luna
Declare top level variable private String username
Eclipse kindly generate a warning on the left of your screen click that warning and couple of suggestions show up, then select generate.

- 113
- 1
- 2