Another Java question: naming the "Getters and Setters," as SL called them. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Another Java question: naming the "Getters and Setters," as SL called them.

I noticed that it says that, in naming the Getters and Setters, a Capital Letter is to be used. So...does that mean that they are a type of Constant (or, um...I think they are called final in Java)?

6th Nov 2017, 4:50 PM
Michael
Michael - avatar
5 Answers
+ 1
finals are all uppercase with underscores if needed. variables are written small in cammelCaseStyle and should contain their own description aka a Noun. methoda are written small in camelCaseStyle but should include a verb which says what the method is doing. getters are: getVariable() for any type and isVariable() for boolean. setters are: setVariable(set type) thats all the convention you should remember for naming things for now. Try avoiding special characters in names and numbers if possible it just looks bad. Numbers are only ok if they simplyfy the name alot. There are more but you'll stumble upon them on your own. Also as long as your questions show some effort in understanding im happy to answer so dont worry. happy coding
7th Nov 2017, 2:34 PM
Jeremy
Jeremy - avatar
+ 3
Constants is all caps and spaced with an underscore. Example: MAX_ELEMENTS I didn't learn Java via SoloLearn, but how I was taught, you just use the same name prefixed with either get or set. EXAMPLE: private String firstName; public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; }
6th Nov 2017, 5:10 PM
AgentSmith
0
you can name getter and setter however you want, it just is common and good practice to name them getNameOfVariable and setNameOfVariable, or for boolean the getter would be isBooleanVariable. But in theory you could also name them "peter" or "ahskcbakdhcbadckh".
6th Nov 2017, 5:16 PM
Jeremy
Jeremy - avatar
0
@Jeremy Yeah, that was my understanding, but it said for these, you must make the variable name begin with a Capital Letter. And, I guess, to further highlight the point, it said it once when talking about Getters and then again when talking about Setters. I dunno, maybe it was just how I happened to read it but it really stuck out for me. Anyway, I really appreciate you taking the time to explain this to me. I don't want you to feel like I was discounting your answer. Actually, quite the opposite: I felt like I wasn't actually going crazy when you validated what I was sure was the original way of naming variables: begin with letters, $this$, or _this_ but no numbers. Included in the rest of the name can be everything I just mentioned but as long as it is NOT the beginning of the name, then numbers can be used too. I think that about does it for variable naming conventions. As far as I know, the Constants (in Java, called final, I think) were the only ones that were Capitalized.
7th Nov 2017, 2:25 PM
Michael
Michael - avatar
0
@Jeremy Okay, cool...I appreciate that. Thanks, BTW, for taking the time to explain that to me. I have to warn you: you may have just bitten off more than you can chew, lol. I can-sometimes-have a tendency to ask lots of questions. LOL No, I'm not too bad. The above IS true, so if I inundate you to the point where you are getting annoyed, please: definitely tell me. I am not easily offended so feel free to say what you need to if it gets that bad. Another thing is that I am so busy right now that I have little time to post a bunch of questions, anyway. So, no worries. All right, man. I gotta get back to the analog world...lol. Hey, thanks again for your help, Jeremy. I'm sure I'll tty soon. Take care. Michael C.
11th Nov 2017, 5:53 PM
Michael
Michael - avatar