import
java.lang.reflect.InvocationTargetException;
import
java.lang.reflect.Method;
class
Student {
private
String name;
private
int
age;
public
Student(String name,
int
age)
{
this
.name = name;
this
.age = age;
}
public
String getName() {
return
name; }
public
void
setName(String name) {
this
.name = name; }
private
int
getAge() {
return
age; }
public
void
setAge(
int
age) {
this
.age = age; }
@Override
public
String toString()
{
return
"Employee [name="
+ name +
", age="
+ age
+
"]"
;
}
}
class
GFG {
public
static
void
main(String[] args)
{
try
{
Student e =
new
Student(
"Kapil"
,
23
);
Method privateMethod
= Student.
class
.getDeclaredMethod(
"getAge"
);
privateMethod.setAccessible(
true
);
int
age = (
int
)privateMethod.invoke(e);
System.out.println(
"Age of Student: "
+ age);
}
catch
(Exception e) {
e.printStackTrace();
}
}
}