Modifiers are keywords added to change the intent of defining a class, variable, method/function or constructor. In Java programming, modifiers are categorized into 2:
- Access Control Modifier
- Non Access Modifier --> *This review will be updated immediately if the material has been fully mastered by the Author.
1. Access Control Modifier
Java programming has 4 access modifiers to control the level of classes, variables, methods/functions and constructors.
- Default: has scope only within the same package
- Public: has a more open scope, so it can be accessed in any class.
- Protected: has scope only within the same package and can be accessed by all sub classes in that package.
- Private: has scope only within the same class.
You can see an example of the implementation of Access Control Modifier in PHP HERE .
Illustration:
NOTE: Example of attribute declaration in a program in the *skip --> public int i module; according to syntax rules (correct) but according to modifier rules (wrong) because everything is naked (public) so it can be accessed by all classes.
WARNING!!
- Never define a public modifier for the ----> attribute.
- Never directly access ---> attributes, example: int i;
- Because it can have a negative impact on the process and output of the class that we have designed.
SOLUTION
- So please use the private modifier for attributes, for example: private int i;
- Use functions to access attributes (insert validation statements) and specify parameters to access them.