Private access modifier in java
The private access modifier limit the scope of there members and methods only for class in which it is declare and define. If class or interface cannot declare as private. it cannot be accessible if it declared as private.

private class

Java allows to define class as private that makes the class accessible only inside the class in which it is declared. If we make class private, it will not be accessible outside the class. A class can be define as private, if a class define as inner private class.
private class
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6         Operation opr = new Operation();
 7         int res = opr.add(10, 20);
 8         System.out.println("An addition of two number : " + res);
 9     }
 10 
 11     private static class Operation {
 12         public int add(int a, int b) {
 13             return a + b;
 14         }
 15     }
 16 }
In the above example, an inner class is define with private access modifier that accessible only inside class Example. An instance of class is created inside method and perform addition by calling an instance of private class Operation. The result is assigned to the variable that will be printed.
Output
 1 An addition of two number : 30

private method

private method
 1 package com.java;
 2 
 3 class Operation {
 4     // accessible only inside class
 5     private int add(int a, int b) {
 6         return a + b;
 7     }
 8 
 9     public int sum(int a, int b) {
 10         // calling private method from class
 11         return this.add(a, b);
 12     }
 13 }
 14 
 15 public class Example {
 16 
 17     public static void main(String[] args) {
 18         Operation opr = new Operation();
 19         int res = opr.sum(10, 20);
 20         System.out.println("An addition of two number : " + res);
 21 
 22     }
 23 }
In the above example, a class Operation is declared with private and public method. A private method accessed from public method of same class and returns the result. An instance of class Operation is created and call sum() method that internally calls private method and return the addition of number. The result is assigned to the variable that will be printed.
Output
 1 An addition of two number : 30

Private member

Private member
 1 package com.java;
 2 
 3 class Operation {
 4     private String type;
 5 
 6     public String getType() {
 7         return type;
 8     }
 9 
 10     public void setType(String type) {
 11         this.type = type;
 12     }
 13 }
 14 
 15 public class Example {
 16 
 17     public static void main(String[] args) {
 18         Operation opr = new Operation();
 19         opr.setType("Addition");
 20         System.out.println("Operation type is : " + opr.getType());
 21     }
 22 }
In the above example, a class Operation declare with private member. A value of property set using setter and retrieved using getter method. An instance of Operation class is created and set value using setType() method and retrieve using getType() method.
Output
 1 Operation type is : Addition
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us