i have been told that declaring variables in a public function in a class is bad. is this true, if so why?
the reason i ask is cause i hate having to declare an array in a private acces specifier, then give it a value in the public one.
The idea is to encapsulate the data. That will loosen the coupling between the classes, and making changes should to be easier.
However get/set methods do break encapsulation.
Classes contain member variables. Sometimes, those member variables are private to the class - that is, they can only be read or changed by the class itself. As such, if the programmer is to read or change those values, they must be given some way to do it. Commonly, this is done by providing class functions that the programmer is allowed to use, and those functions change/read the value for the programmer. They are often referred to as a getter (reading the value) or setter (changing the value).