티스토리 뷰
728x90
import java.lang.reflect.Field;
public class ToStringMaker {
public ToStringMaker( Object o) {
Class<? extends Object> c = o.getClass();
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
String name = field.getName();
field.setAccessible(true);
try {
System.out.format("%n%s: %s", name, field.get(o));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}}
https://stackoverflow.com/questions/44165554/use-reflection-api-to-get-field-names-and-values
// Access Private Field Using Reflection in Java
import java.lang.reflect.Field;
// Student class declaration
class Student {
// private fields
private String name;
private int age;
// Constructor
public Student(String name, int age)
{
this.name = name;
this.age = age;
}
// Getters and setters
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 toString method to get required
// output at terminal
@Override public String toString()
{
return "Employee [name=" + name + ", age=" + age
+ "]";
}
}
// Program will throw an exception on online IDE
// Compile and run the program on offline IDE
class GFG {
public static void main(String[] args)
{
try {
// Student object created
Student e = new Student("Kapil", 23);
// Create Field object
Field privateField
= Student.class.getDeclaredField("name");
// Set the accessibility as true
privateField.setAccessible(true);
// Store the value of private field in variable
String name = (String)privateField.get(e);
// Print the value
System.out.println("Name of Student:" + name);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
https://www.geeksforgeeks.org/how-to-access-private-field-and-method-using-reflection-in-java/
728x90
'공부' 카테고리의 다른 글
[Regex] id, pw, email (0) | 2022.02.13 |
---|---|
[Kafka] research (0) | 2022.02.04 |
[Tableau] 날짜 함수 (0) | 2022.02.04 |
[Spark] write mode overwrite (0) | 2022.01.16 |
[Java] delete topic (0) | 2022.01.16 |
댓글
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 테슬라 레퍼럴 적용 확인
- 테슬라 추천
- wlw
- Bot
- 테슬라 리퍼럴 코드 생성
- 테슬라 리퍼럴 코드
- 레퍼럴
- COUNT
- 인스타그램
- 유투브
- 메디파크 내과 전문의 의학박사 김영수
- 테슬라 레퍼럴
- 모델y
- Kluge
- 클루지
- 김달
- 테슬라 레퍼럴 코드 확인
- 어떻게 능력을 보여줄 것인가?
- 할인
- 팔로워 수 세기
- 테슬라
- 개리마커스
- 모델 Y 레퍼럴
- 연애학개론
- 테슬라 리퍼럴 코드 혜택
- 책그림
- follower
- 테슬라 크레딧 사용
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
글 보관함