java get attribute / property

‮www‬.lautturi.com
java get attribute / property
/**
 * @author lautturi.com 
 * Java example: get attribute in java class
 */

class Person{
	public String name;
	int age;
	
	public Person(String string, int i) {
		name = string;
		age = i;
	}

	public String getName() {
		return name;
	}
}

public class Lautturi {

	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {

		Person obj = new Person("lau",23);
		
		//normal
		String value = obj.name;

		//runtime
		Object x = obj.getClass().getField("name").get(obj);
		
		System.out.println(x);

	}
}

output:

lau
Created Time:2017-09-23 13:10:51  Author:lautturi