Java-Private methods

Private methods

Variables are generally declared with the access control modifier private. In such a case, they can be accessed only by methods of the class in which they are defined. Methods are rarely declared to be of type private. Such a method can be invoked only by the methods in the class in which that method is defined.

Program

final

Program Source

class Data{
    
    private void privateMethod()
    {
        System.out.println("Private Method Invoked");
    }
    
    void accessPrivateMethod()
    {
        privateMethod();
    }
}

public class Javaapp {

    public static void main(String[] args) {
       
        Data obj  = new Data();
        obj.accessPrivateMethod();
    }
}

Leave a Comment