A modifier just as the name implies is said to transform , change or customize another element in the structure, on which it is dependent. When you think of modifiers, think of the role of an adjective in a phrase- just as adjective modifies a noun in spoken languages, the modifier modifies the element its dependent on. Also, just as a phrase without an adjective can exist without losing its meaning (you can say “a girl” instead of saying “a beautiful girl” ), a Java keyword can exist without a modifier; a modifier only gives more details to its dependent. Java modifiers can be classed into two as listed below with the types under each category
- private: This is Mr. selfish, when a class marks a method or variable as private it simply means “I’m not willing to share,thank you.”. Any method or variable marked as private is guarded jealously by the owner class hence can only be accessed within the class.This is not shared with any other class within and outside the package and also the sub-classes.
- default: When no modifier is specified for a keyword, default modifier is used. This modifier makes its keyword accessible to just the class and the package where its declared hence they are sometimes referred to as package modifier. This is not visible to sub-classes and the world.
- protected: Modifiers defined with protected modifier is visible to the defining class, the classes with the package and also sub-classes. This modifier is only not visible to the world.
- public: This is Mr. Generous, a keyword defined with this modifier is visible to all – the defining class, classes within the same package, sub-classes and the world.
So, why do we need to specify an access modifier? We need it to achieve one of Object Oriented Programming (OOP) concept called Encapsulation also known as data hiding.
Encapsulation: Its one of the OOP concept that aims at protecting valuable information so as to hide unnecessary details. For instance a variable made public can be modified by any class that is why variable should never be made public but methods can be made public so as to allow other classes access to use it.
- static: Static methods and variables are associated with the class. They are defined with the static keyword and can be accessed using class name. Non static methods can’t access a static method
- final: Just as the name implies, final values are used when the variable value can’t change. A final class cannot be extended, a final method’s behaviour cannot change and a final variable value cannot change.
- abstract: When a class is defined as abstract it means it can only by extended and not instantiated. An abstract class can contain both abstract method and non abstract method but a class cannot be both abstract and final.
- synchronized and volatile: These modifiers are used with thread. A thread is the path followed when executing a program. All Java programs have at least one thread, known as the main method which is created by the JVM at the program’s start, when the main() method is invoked with the main thread.
Photo Credit: Stackoverflow