3

Question

Suppose there is a private variable in a public class called private int _xTouchDown = 0;

What would be appropriate for the getter/setter name for this private variable?

a. get_xTouchDown() for the getter and set_xTouchDown() for the setter. These are the default chosen by Eclipse > Refactor > Encapsulate Field.

b. getXTouchDown() for the getter and setXTouchDown() for the setter.

c. xTouchDown() for the getter and setXTouchDown() for the setter.

d. Other.

3
  • 1
    its entirely a matter of preference/project guidelines. where i work, for example, you would never get away with starting a variable name with an underscore to begin with.
    – radai
    Jul 10, 2013 at 5:30
  • 1
    There is no one answer to that. Just name them the way you want. You only need to stick to javabean convention if you use {de,}serialization of some sort, and even then, worthy frameworks will give you options to not use classical setters.
    – fge
    Jul 10, 2013 at 5:31
  • its b, the second option :)
    – Charan Pai
    Jul 10, 2013 at 5:52

6 Answers 6

2

There is no mandatory name. But the convention is if a variable name is myVariable then the getter and setter should be getMyVariable or setMyVariable. Here Note that your variable name is not conventional either.

So I think before following the convention of getter and setter it is more important to follow the convention of variable naming.

you can also check the code generation of getter and setter using a IDE. that will probably follow the convention.

To learn about the java naming convention see this doc. Specially Chapter 9

0
1

I would say:

getXTouchDown() for the getter and setXTouchDown() for the setter.

Why?

It obeys Java Naming Conventions and more important:

Getters and Setters are for the outside world. Client code using your methods should not care for how your fields are named internally.

1
  • Thanks for the reference link, that's what I'm looking for!
    – George
    Jul 10, 2013 at 6:50
1

getter:

getXTouchDown()

setter:

setXTouchDown()
1

Generally your IDE (Eclipse, Netbeans, etc) can automatically generate the getter and setter. Typically they would name it scenario (A) as you listed above.

However, as the other posts stated, I would recommend going with scenario (B) since that's more common with regards to variable naming. Why not just name your original variable "xTouchDown" and let your IDE autogenerate the code for you?

Also, you don't need to initialize your instance variable if you want it to be 0; that's already the default for instance variables (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

1
  • Your explanation is great! I name the private variable with a underscore prefix because I saw some other programmers use it for private variable in their Android project. So I follow suit.
    – George
    Jul 10, 2013 at 6:59
1

Getters and Setters are just the preferences of your own choice. You can write the names of the methods any way and anything. Though the convention used everywhere is :

Getter :- getMyVariable

Setter :- setMyVariable

By default in IDEs _ and such other things are removed.

1

Even i have never tried to manually give the name to getter and setter

For Eclipse

It is easiest to auto generate such getters & setters or constructors too

Method One:

  1. just select the variable Name then
  2. Right Click > Source > Generate Getters & Setters

Method Two

  1. Select Variable Name
  2. Alt+Shift + S > Generate Getters & Setters

Select what you want to generate It will generate automatically as defined in code template modify as per need

For Other IDE

You can find something like this hopefully they will be providing this

Not the answer you're looking for? Browse other questions tagged or ask your own question.