Class 클래스
- 자바의 모든 클래스와 인터페이스는 컴파일 후 class 파일이 생성됨
- Class 클래스는 컴파일 된 class 파일을 로드하여 객체를 동적 로드하고, 정보를 가져오는 메서드가 제공됨
- Class.forName("클래스 이름") 메서드로 클래스를 동적으로 로드 함
Class c =Class.forName("java.lang.String");
- 클래스 이름으로 직접 Class 클래스 가져오기
Class c =String.class;
- 생성된 인스턴스에서 Class 클래스 가져오기
String s= new String();
Class c = s.getClass(); //Object 메서드
동적로딩
- 컴파일 시에 데이터 타입이 binding 되는 것이 아닌, 실행(runtime) 중에 데이터 타입을 binding 하는 방법
- 프로그래밍 시에는 문자열 변수로 처리했다가 런타임시에 원하는 클래스를 로딩하여 binding 할 수 있다는 장점
- 컴파일 시에 타입이 정해지지 않으므로 동적 로딩시 오류가 발생하면 프로그램의 심각한 장애가 발생가능
Class의 newInstance() 메서드로 인스턴스 생성
- new 키워드를 사용하지 않고 클래스 정보를 활용하여 인스턴스를 생성할 수 있음
클래스 정보 알아보기
- reflection 프로그래밍 : Class 클래스를 사용하여 클래스의 정보(생성자, 변수, 메서드)등을 알 수 있고 인스턴스를 생성하고, 메서드를 호출하는 방식의 프로그래밍
- 로컬 메모리에 객체 없는 경우, 원격 프로그래밍, 객체의 타입을 알 수 없는 경우에 사용
- java.lang.reflect 패키지에 있는 클래스를 활용하여 프로그래밍
- 일반적으로 자료형을 알고 있는 경우에 사용하지 않음
예제
- StringTest.java
public class StringTest {
public static void main(String[] args) throws ClassNotFoundException {
Class c = Class.forName("java.lang.String");
Constructor[] cons = c.getConstructors();
for(Constructor co : cons) {
System.out.println(co);
}
Method [] m = c.getMethods();
for(Method mth : m) {
System.out.println(mth);
}
}
}
- Person.java
public class Person {
private String name;
private int age;
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return name;
}
}
- ClassTest.java
public class ClassTest {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Class c1 = Class.forName("ch04.Person");
Person person = (Person)c1.newInstance();
person.setName("하위");
System.out.println(person);
Class c2 = person.getClass();
Person p = (Person)c2.newInstance();
System.out.println(p);
Class[] parameterTypes = {String.class};
Constructor cons = c2.getConstructor(parameterTypes);
Object[] initargs = {"kim"};
Person kimPerson = (Person)cons.newInstance(initargs);
System.out.println(kimPerson);
}
}
'프로그래밍 언어 > JAVA(자바) 응용' 카테고리의 다른 글
41.T extends 클래스 (0) | 2022.05.27 |
---|---|
40. 무엇이든 담을 수 있는 제네릭(Generic)프로그래밍 (0) | 2022.05.27 |
38.String, StringBuilder, StringBuffer 클래스, text block (0) | 2022.05.25 |
37.Object 클래스의 메서드 활용 (0) | 2022.05.25 |
36.Object클래스 - 모든 클래스의 최상위 클래스 (0) | 2022.05.25 |
댓글