What is Modifier and what are the applicable Class Modifiers?
Modifiers are keywords that you add to class, methods, and variables to change their meanings. Java language has a wide variety of modifiers. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components. When a class is declared as public, it is accessible to other classes defined in the same package as well as those defined in other packages. This is the most commonly used specifier for classes. A top-class cannot be declared as private.
Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have. We can say there is two type of modifiers in java.
- Access specifiers
- Access modifiers
- Public, private, protected, default is considered as specifiers except these remaining are considered as modifiers.
- But this rule is applicable only for old language like C++ but not in Java.
- In Java, all are considered as modifiers only there is no word like specifiers.
Whenever we are writing our own classes we have to provide some info about our class to the JVM like
- whether this class can be accessed from anywhere or not.
- Whether child class creation is possible or not.
- Whether object creation is possible or not etc.
- We can specify this info by using appropriate modifiers.
The only applicable modifiers for top-level classes are
- Public, Default, Final, Abstract, strictfp
But for inner classes the applicable modifiers are
- Public, Protected, Private, Static, Final, Abstract, strictfp