Advertisement



< Prev
Next >



Class Member Access Modifiers






The components of a class, such as its instance variables or methods are called the members of a class or class members. A class member is declared with an access modifier to specify how it is accessed by the 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 its class is visible to other classes. Let us see an 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 class B, by using an object of class A. Hence, there is no compile error when B.java is 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


    The compiler shows a compile error which describes that an int member variable of A class, which has private access, cannot be used outside its class.


    Advertisement




  • 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 -



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON and DJANGO


    Advertisement