티스토리 뷰

Java

Reflection

techbard 2016.07.22 11:33



# 정의


Reflection is a way to invoke methods of objects on the fly (At run-time).

Reflection is the ability, at runtime, to actually create objects of classes, invoke methods, manipulate meta data.



- Reflection is available in most important languages (java, C# etc)

- Reflection is slow and complicated

- A method call via reflection may take 10X longer than usual



# code


import java.util.ArrayList;


public class Main {


    public static void main(String[] args) 

   throws InstantiationException, IllegalAccessException, 

   ClassNotFoundException

    {

ArrayList<String> al = new ArrayList<>();

ArrayList<String> onTheFlyList = 

(ArrayList)

(Class.forName("java.util.ArrayList").newInstance());

onTheFlyList.add("a");

onTheFlyList.add("b");

for (String string : onTheFlyList) {

   System.out.println(string);

}

    }

}


댓글
댓글쓰기 폼
공지사항
Total
394,929
Today
0
Yesterday
28
«   2018/11   »
        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  
글 보관함