[기술 정리] 5. 익명 클래스 (Anonymous Class)

KangHo Lee's avatar
Nov 21, 2024
[기술 정리] 5. 익명 클래스 (Anonymous Class)
💡
익명 클래스는 이름이 없는 클래스이며, 주로 일회성으로 사용되거나 특정 기능을 구현하기 위해 빠르게 정의되고 사용됩니다.
public class AnonymousClassExample { public static void main(String[] args) { // Runnable 인터페이스를 구현하는 익명 클래스 Runnable runnable = new Runnable() { @Override public void run() { System.out.println("익명 클래스 스레드 실행 중..."); } }; // 스레드 생성 및 실행 Thread thread = new Thread(runnable); thread.start(); } }
  • Runnable 인터페이스 구현
    • Runnable 인터페이스를 구현하는 익명 클래스를 정의합니다.
    • run 메서드를 오버라이드하여 스레드가 실행될 때 수행할 작업을 정의합니다.
  • 스레드 생성 및 실행:
    • 익명 클래스로 구현된 Runnable 객체를 사용하여 Thread 객체를 생성합니다.
    • start 메서드를 호출하여 스레드를 실행합니다.
 
Share article

devleekangho