[static] 수학

seratpfk·2022년 7월 28일
0

JAVA

목록 보기
50/96

static

  1. 정적 요소
  2. 객체 생성 이전에 메모리에 미리 만들어 지는 공유 요소
  3. 클래스에서 1개만 만들어 짐
  4. 클래스를 이용해서 호출하기 때문에 클래스 변수, 클래스 메소드라고 부름.

MyMath 클래스 생성 (메인메소드 없음)

// 필드
public static final double PI = 3.141592;
// 메소드
public int abs(int n) {
	return (n >= 0) ? n : -n;
}
public static int pow(int a, int b) {
	int result = 1;
	for(int cnt = 0; cnt < b; cnt++) {
		result *= a;
	}
	return result;
}
  • final은 못바꾸기 때문에 public으로 해도 못 바꿈.

MyMath(메인메소드 설정)

System.out.println(MyMath.PI);
System.out.println(MyMath.abs(-5));
System.out.println(MyMath.pow(2, 5));  // 2의 5제곱(32)

출력:
3.141592
5
32

  • 수학은 객체를 만들 필요는 없음.
    Math math = new Math(); -> 입력 안해도 상관 없음.

0개의 댓글