웹서버 프로그래밍 (node.js)/자바 프로그래밍

자바 Class 내부 Method 선언 및 내부 Method Call

kkedory 2020. 3. 2. 13:56
728x90

자바에서는 Class 내에 Method를 선언하고 외부에서 Method를 Call할 수 있습니다.

 

프로그램

 

public class method_call {

       static String myMethod() {
               return "hello world 2";
        }

        public static void main(String[] args) {
              System.out.println("hello world 1");

              String text = myMethod();

              System.out.print(text);
        }

 

}

예상결과

 

hello world 1
hello world 2

 

풀이

 

1. 메소드를 call하면 문자 String을 되돌려주는 메소드 정의

 

        static String myMethod() { 
               return "hello world 2"; 
        }  

 

2. 메인 Process가 메소드를 Call 하고 결과 String을 로컬 스트링으로 받음, 받은 스트링을 출력

 

              String text = myMethod();

              System.out.print(text); 

728x90