The name of getter and setter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

The name of getter and setter

An I correct that I am free to decide on my own the names of my getters and setters? Now the tutorial is teaching me to name my getter “getVar” capitalizing the first character of the variable, and similarly for setter. But what I see is that it is just a new method defined. It is the “return Var” and “Var = …” defined under those methods that do the job. Are there any reason to stick to that particular naming style?

9th Aug 2016, 6:57 AM
Ka Ho Hui
Ka Ho Hui - avatar
1 Answer
+ 5
Exactly. Common misconception of getters and setters is that they need to be named get/set. They don't have to be, Now, on that note, it is best to put some variation of get and set in the method name so YOU and anyone reading the code knows what those methods are doing. Also, common Java practice to name variables camelCase. Also other etiquettes exist for other things too.
9th Aug 2016, 7:07 AM
James - avatar