How to Get Private Attributes in Java


In this example we will show the method to get private attributes in Java.

Source Code

package com.beginner.examples;

public class Girl {
  private String name = "Lucky";
  private int age = 16;
  
  public static void main(String[] args) {
    Girl g = new Girl();
    // get private attributes
    System.out.println("Name: " + g.name);
    System.out.println("Age: " + g.age);
  }
}

Output:

Name: Lucky
Age: 16
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments