Open In App

Access and Non Access Modifiers in Java

Java provides a rich set of modifiers. They are used to control access mechanisms and also provide information about class functionalities to JVM. They are divided into two categories namely:

  1. Access modifiers
  2. Non-access modifiers



Access Modifiers

Java’s access modifiers are public, private, and protected. Java also defines a default access level (called package-private).

Types of Access Modifiers

Note: 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.



class GFG 
{
public static void main(String[] args) 
    { 
        // Insert your code here 
    }
}    

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 as follows:

  1. static
  2. final
  3. abstract
  4. synchronized
  5. transient
  6. volatile
  7. native

 

Article Tags :