Java

자바에서 메소드 내부에 메소드를 못 만드는 이유

asd135 2023. 10. 19. 00:35
728x90

메소드 내부에 메소드를 만들 수 없다.

자바는 클래스 기반의 객체 지향 프로그래밍 언어이며 모든 메소드는 클래스 레벨에서 선언되어야 한다. 


오류

public class HelloWorld {
    public static void main(String[] args) throws Exception{
        public void Method1() { 
            System.out.println("Java");
        }
    }
}

 

수정

public class HelloWorld {
    public static void main(String[] args) throws Exception{
    
    }

    public void Method1() { 
        System.out.println("Java");
    }
}