상세 컨텐츠

본문 제목

Reflection

Java

by techbard 2016. 7. 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);

}

    }

}


반응형

관련글 더보기

댓글 영역