Make everything as private as possible
All variables, methods and classes should be designated as private unless there is a very good reason for them not to be. A variable, method or class that is designated as public can be executed or accessed by any class at any time. Every public variable, class or method is another way for someone to access your code. Closing these doors is essential to securing your code. These access points are essential for a functional application but they should be severely limited. Only methods, and classes that are essential for the codesŐ public interface should be designated as public.[2] All variables, except in rare circumstances, should be designated as private and should be accessed through getter and setter methods. This will prevent access to variables that should not be accessed externally. These methods can also prevent unauthorized classes from accessing the variables if properly written. Another advantage of a setter method is that the author of the class can ensure the variable is in a valid state by screening the passed value before the variable is set.[12]