티스토리 뷰
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
Use reflection API to get field names and values
I'm creating a number of POJOs and have to create toString() methods for each. Instead of hand-coding each, I thought it would be easier to create a class to do the work for me using the reflection...
stackoverflow.com
// 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/
How to Access Private Field and Method Using Reflection in Java? - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
'공부' 카테고리의 다른 글
[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 |
- Total
- Today
- Yesterday
- 유투브
- 인스타그램
- 클루지
- COUNT
- 연애학개론
- 메디파크 내과 전문의 의학박사 김영수
- 테슬라 리퍼럴 코드 생성
- 테슬라
- 테슬라 레퍼럴 적용 확인
- 모델y
- wlw
- Kluge
- follower
- 어떻게 능력을 보여줄 것인가?
- 테슬라 크레딧 사용
- 테슬라 리퍼럴 코드
- 할인
- 팔로워 수 세기
- 책그림
- 모델 Y 레퍼럴
- 레퍼럴
- 김달
- 테슬라 추천
- 개리마커스
- 테슬라 레퍼럴 코드 확인
- 테슬라 리퍼럴 코드 혜택
- Bot
- 테슬라 레퍼럴
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |