The temptation for methods and member variables is to declare them all public or public static to avoid “annoying” access problems.  Believe it or not, visibility controls exist to simplify development, to reduce the number of methods a developer needs to see when using a class externally.

private is for methods and member variables for use only within the class

public is for methods and member variables that need to be visible to other classes

public static is for methods and member variables that only one instance is needed for.  Public static methods are usually for utility methods only, such as truncating text to a system display limit.

blog comments powered by Disqus