MCPcopy Create free account
hub / github.com/castello/spring_basic / main

Method main

ch2/PrivateMethodCall.java:6–20  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

4
5public class PrivateMethodCall {
6 public static void main(String[] args) throws Exception{
7// Hello hello = new Hello();
8// hello.main(); // private이라서 외부 호출 불가
9
10 // Reflection API를 사용 - 클래스 정보를 얻고 다룰 수 있는 강력한 기능제공
11 // java.lang.reflect패키지를 제공
12 // Hello클래스의 Class객체(클래스의 정보를 담고 있는 객체)를 얻어온다.
13 Class helloClass = Class.forName("com.fastcampus.ch2.Hello");
14 Hello hello = (Hello)helloClass.newInstance(); // Class객체가 가진 정보로 객체 생성
15 Method main = helloClass.getDeclaredMethod("main");
16 main.setAccessible(true); // private인 main()을 호출가능하게 한다.
17
18 main.invoke(hello); // hello.main()와 같은 의미. main()이 private인데도 호출성공
19
20 }
21}

Callers

nothing calls this directly

Calls 1

invokeMethod · 0.80

Tested by

no test coverage detected