is setter method is always void? can't we make it int type and return? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

is setter method is always void? can't we make it int type and return?

20th Jul 2018, 5:05 PM
mn121
mn121 - avatar
4 Answers
+ 8
Conventionally, setters should not return a value. Therefore, it needs to be a void method, because you're not returning anything from it (just initializing the instance variables). Getters, on the other hand, does return a value.
20th Jul 2018, 5:22 PM
Dev
Dev - avatar
+ 1
but is it necessary to write void
20th Jul 2018, 5:12 PM
mn121
mn121 - avatar
0
There's no point, the whole idea of a setter is to set a variable value
20th Jul 2018, 5:09 PM
TurtleShell
TurtleShell - avatar
0
No, setters are like any other function they just have a fancy name
20th Jul 2018, 5:16 PM
TurtleShell
TurtleShell - avatar