Example 1: Access private fields using getter and setter methods
class Test {
  // private variables
  private int age;
  private String name;
  // initialize age
  public void setAge(int age) {
    this.age = age;
  }
  // initialize name
  public void setName(String name) {
    this.name = name;
  }
  // access age
  public int getAge() {
    return this.age;
  }
  // access name
  public String getName() {
    return this.name;
  }
}
class Main {
  public static void main(String[] args) {
    // create an object of Test
    Test test = new Test();
    // set value of private variables
    test.setAge(24);
    test.setName("Programiz");
    // get value of private variables
    System.out.println("Age: " + test.getAge());
    System.out.println("Name: " + test.getName());
  }
}
Output
Age: 24 Name: Programiz
In the above example, we have private variables named age and name. Here, we are trying to access the private variables from other class named Main.
We have used the getter and setter method to access the private variables. Here,
- the setter methods setAge()andsetName()initializes the private variables
- the getter methods getAge()andgetName()returns the value of private variables
Example 2: Access the private field and method using Reflection
import java.lang.reflect.*;
class Test {
  // private variables
  private String name;
  // private method
  private void display() {
    System.out.println("The name is " + name);
  }
}
class Main {
  public static void main(String[] args) {
    try {
      // create an object of Test
      Test test = new Test();
      // create an object of the class named Class
      Class obj = test.getClass();
      // access the private variable
      Field field = obj.getDeclaredField("name");
      // make private field accessible
      field.setAccessible(true);
      // set value of field
      field.set(test, "Programiz");
      // get value of field
      // and convert it in string
      String value = (String)field.get(test);
      System.out.println("Name: " + value);
      // access the private method
      Method[] methods = obj.getDeclaredMethods();
      System.out.println("Method Name: " + methods[0].getName());
      int modifier = methods[0].getModifiers();
      System.out.println("Access Modifier: " + Modifier.toString(modifier));
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
}
Output
Name: Programiz Method Name: display Access Modifier: private
In this example, we have a private field named name and a private method named display(). Here, we are using the reflection to access the private fields and methods of the class Test.
To learn about reflection, visit Java Reflection.