Java provides a rich set of modifiers. They are used to control access mechanism and also provide information about class functionalities to JVM. They are divided into two categories : –
- Access Modifiers : Java’s access modifiers are public, private, and protected. Java also defines a default access level (called package-private).
How they work?
public: When a member of a class is modified by public, then that member can be accessed by any other code.
private: When a member of a class is specified as private, then that member can only be accessed by other members of its class.
Now you can understand why main( ) has always been preceded by the public modifier. It is called by code that is outside the program—that is, by the Java run-time system. When no access modifier is used, then by default the member of a class is public within its own package, but cannot be accessed outside of its package. protected applies only when inheritance is involved.
Detailed article : Access Modifiers in Java
- Non-access modifiers : In java, we have 7 non-access modifiers. They are used with classes, methods, variables, constructors etc to provide information about their behavior to JVM.They are
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Access Modifiers in Java
- Access specifiers for classes or interfaces in Java
- More restrictive access to a derived class method in Java
- Java | CDMA (Code Division Multiple Access)
- Access specifier of methods in interfaces
- Method Overriding with Access Modifier
- Spring Boot | How to access database using Spring Data JPA
- Modifiers interfaceModifiers() method in Java with Examples
- Modifiers toString() method in Java with Examples
- Modifiers methodModifiers() method in Java with Examples
- Modifiers parameterModifiers() method in Java with Examples
- Modifiers classModifiers() method in Java with Examples
- Modifiers fieldModifiers() method in Java with Examples
- Modifiers constructorModifiers() method in Java with Examples
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples