< Prev
Next >



Class Member Access Modifiers






The components of a class such as its instance variables or methods are called members of a class i.e. class members. A class member is declared with an access modifier, that specifies how it will be accessed by other classes in Java. A Java class member can take any of the access modifiers, such as - public, protected, default and private.




Public access modifier


A class member, be it an instance variable or a method can be declared with the public keyword. A class member declared with a public access modifier is accessible and visible to all the classes, as long as the class in which this public member is declared, is visible to other classes. Let's see example-


A.java
public class A
{
public void m()
{
System.out.println("A's method is called");
}
}



B.java
class B
{
public static void main(String... ar)
{
A ob= new A();
ob.m();
}
}


Output is :


A's method is called


Program Analysis


In this code, we have created two classes, A(in A.java)and B(in B.java) Class A has a method m() which is declared public and that's why it is visible and accessible in the class B, using the object of class A. Hence, there is no compile error when B.java was compiled.




  • Private access modifier


  • A class member - instance variable or method declared with the private keyword will be hidden and inaccessible to the other classes and it won't serve any purpose. Private member of a class is only accessible in the class in which it is declared or defined and not outside it. Let's see an example -

    A.java
    public class A
    { 
    private int a=10;
    }
    
    
    B.java
    class B
    {
    public static void main(String... ar)
    {
    A ob= new A();
    System.out.println(ob.a);
    }
    }
    


    Output is -


    A7.java:6: error: i has private access in A
    System.out.println(ob.i);
                         ^
    1 error


    Program Analysis


    Compiler shows a compile error which clearly states that int member variable of A class has a private access, hence it can't be used outside class A(in which it is declared and defined.)





  • Default access modifier


  • When you declare a class member with no access modifier, it is automatically given a default access modifier or default visibility, which means that this class member is accessible and visible only to the classes in the same package.

    Let's understand this by an example -


    Accessing a class member with default access



    A.java
    package pack1;
    
    public class A
    { 
    int num=10; 		//a is declared with no access modifier, hence, it has default access modifier
    }
    
    
    B.java
    package pack1;
    
    class B
    {
    public static void main(String... ar)
    {
    A ob= new A();
    System.out.println(ob.a);
    }
    }
    


    Output is -:


    10


    Program Analysis


    Class A and class B exist in the same package, pack1, hence, when the member variable, num(which has default access) of class A was accessed in the code of B class, we have received no compile error in the compilation of B.java.




  • Protected access modifier


  • A Java class member such as - instance variable or method declared with the protected keyword will be accessible and visible to -
    Please subscribe our social media channels for notifications, we post a new article everyday.

    Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

    Coming Next
    -
    C++ and Python

    Ad2