How to Get Public Attributes in Java


In this example we will show you how to get public attributes in Java.

Source Code

package com.beginner.examples;

class Person {
  public String name = "Lucky";
  public int age = 16;
}

class Lucky {

  public static void main(String[] args) {
    Person p = new Person();
    // get public attributes
    System.out.println("Name: " + p.name);
    System.out.println("Age: " + p.age);
  }
}

Output:

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