
package com.haenin.section01.method;
public class Application1 {
public static void main(String[] args) {
/* 목표, 메소드의 호출 흐름(Method Call)에 대해 이해할 수 있다. */
/* 필기. 메소드가 스택으로 쌓임 */
System.out.println("main() 시작함...");
methodA(); // 필기. 원래 static메소드는 클래스명.메소드명() 해야하지만
Application1.methodA(); // 필기. Static메소드가 같은 클래스인 경우 생략 가능
System.out.println("main() 종료됨...");
}
public static void methodA(){
System.out.println("methodA() 호출함...");
methodB();
System.out.println("methodA() 종료함...");
}
public static void methodB(){
System.out.println("methodB() 호출함...");
System.out.println("methodB() 종료함...");
}
}
package com.haenin.section01.method;
public class Application2 {
public static void main(String[] args) {
System.out.println("main() 시작함...");
/* 설명. static method일 경우 */
/* 필기. 자바가 처음 시작할 때 인지되는 것이 static */
// methodA();
// methodB();
/* 설명. non-static method일 경우 */
System.out.println("main() 종료됨...");
Application2 app = new Application2();
app.methodA(); // 필기. 접근연산자(.)를 활용해서 접근해서 호출
app.methodB();
}
public void methodA(){
System.out.println("methodA() 호출함...");
System.out.println("methodA() 종료함...");
}
public void methodB(){
System.out.println("methodB() 호출함...");
System.out.println("methodB() 종료함...");
}
}
package com.haenin.section01.method;
public class Application3 {
/* 설명. 전역변수(메소드들에서 쓸 수 있는 변수) */
static int global = 10;
public static void main(String[] args) {
/* 목표, 메소드 전달인자(argument)와 매개변수(paramete)에 대해 이해하고 활용할 수 있다. */
/* 설명.
* 변수를 선언한 위치에 따라 변수는 구별해서 부를 수 있다.
* 1. 지역변수
* 2. 전역변수
* 3. 매개변수
* 4. 클래스 변수 ( 추후에 다룰 예정 )
* 5. 인스턴스 변수 ( 추후에 다룰 예정 )
* */
System.out.println("전역 변수: "+ global);
int local = 20;
System.out.println("지역 변수: "+ local);
int age = 26;
Application3 app = new Application3();
app.testMethod(age);
app.testMethod(age+1);
app.testMethod(age+2);
}
/* 설명. int age는 매개변수이자 지역변수 */
public void testMethod(int age){
System.out.println("당신의 나이는 " + age + "세 입니다. ");
}
}
package com.haenin.section01.method;
public class Application4 {
public static void main(String[] args) {
/* 목표, 여러 개의 전달인자를 이용한 메소드 호출을 할 수 있다. */
Application4 app4 =new Application4();
app4.testMethod("최혜원",26,'여');
String name ="차은우";
int age = 20;
char gender = '남';
app4.testMethod(name,age,gender);
}
public void testMethod(String name, int age, final char gender){
System.out.println("당신의 이름은 "+name+"이고, 나이는 "+age+"세 이며, 상뱔은 "+gender+ "입니다.");
return;
// 필기. void메소드는 return;이 원래 있지만 우리가 생략하는것
}
}
package com.haenin.section01.method;
public class Application5 {
public static void main(String[] args) {
/* 목표, 메소드의 리턴에 대해 이해할 수 있다. */
Application5 app5 = new Application5();
app5.testMethod();
String returnStr = app5.returnMethod();
System.out.println("returnStr = " + returnStr);
/* 설명. 반환값은 변수에 담지 않아도 된다. */
System.out.println("반환값 바로 확인 = " + app5.returnMethod());
}
/* 설명. 반환이 없는 경우 */
public void testMethod() {
System.out.println("testMethod() 동작 확인...");
return; // 컴파일러가 작성해줌
}
/* 설명. 반환을 String으로 하는 경우 */
public String returnMethod() {
System.out.println("returnMethod() 동작 확인...");
return "test";
}
}
package com.haenin.section01.method;
public class Application6 {
public static void main(String[] args) {
/* 목표, 다른 클래스에 작성한 메소드를 활용할 수 있다. */
Calculator calc = new Calculator();
int first = 100;
int second = 50;
/* 설명. 덧셈기능 */
System.out.println("두 수의 합은 ? "+ calc.plusTwoNumbers(first,second));
/* 설명. 최솟값 */
System.out.println("두 수 중 최소값은 ? "+ calc.minTwoNumbers(first,second));
/* 설명. 최댓값 */
System.out.println("두 수 중 최댓값은 ? "+ Calculator.maxTwoNumbers(first,second));
}
}
package com.haenin.section01.method;
public class Calculator {
public int plusTwoNumbers(int first, int second) {
return first + second;
}
public int minTwoNumbers(int first, int second) {
return (first > second) ? second : first;
}
/* 설명. static method 일 경우 */
/* 필기. static이라면 Calculator(클래스이름).maxTwoNumbers 가능 */
public static int maxTwoNumbers(int first, int second) {
return (first > second) ? first : second;
}
}