After I learned to properly use private
protected
abstract
sealed
in a language like C# I found no reason to make a variable public
ever again.
An interface to the variable is usually a public
method if it is an action/verb (e.g. Car.Move()
), or if it is a value/property of the class then I use property with a public
getter and a private
setter. (e.g. Car.Color
which can only change on creation, but accessible anytime.)
If it is just a collection of values, its field still make more sense to be private
and only assignable on creation via the constructor.
I want to know some situations which a common practice is to make a public variable if you have any to share.
public variable
? A public instance attribute? Or public class attribute? – Laiv Mar 23 '18 at 14:35