JAVA_ CLASS

장성욱·2025년 6월 2일

JAVA

목록 보기
7/23

static

  • class에 속하는 것을 의미함

  • 객체를 만들지 않고 클래스 이름으로 바로 접근이 가능

  • 공통으로 사용되는 메서드나 변수에 적합함


계산기 클래스


class Main {
  public static void main(String[] args) {
    int 결과;

    결과 = 계산기.(10, 20);
    System.out.println("결과 : " + 결과);
    // 출력 => 결과 : 30

    결과 = 계산기.(30, 20);
    System.out.println("결과 : " + 결과);
    // 출력 => 결과 : 50

    결과 = 계산기.(30, 70);
    System.out.println("결과 : " + 결과);
    // 출력 => 결과 : 100

    결과 = 계산기.(30, 70);
    System.out.println("결과 : " + 결과);
    // 출력 => 결과 : -40

    결과 = 계산기.(3, 7);
    System.out.println("결과 : " + 결과);
    // 출력 => 결과 : 21
  }
}

class 계산기 {
  static int(int a, int b) {
    return a+b;
  }

  static int(int a, int b) {
    return a-b;
  }

  static int(int a, int b) {
    return a*b;
  }
}

합을 구하는 클래스

  • 1부터 지정된 수까지

class Main {
  public static void main(String[] args) {
    int rs_1 = math.onetosum(3);
    System.out.println("결과 : " + rs_1);

    int rs_2 = math.onetosum(10);
    System.out.println("결과 : " + rs_2);
  }
}

class math {
  static int onetosum(int a) {
    int s = 0;
    for (int i = 1 ; i <= a; i ++) {
      s += i;
    }
    return s;
  }
}
  • a부터 b까지
class Main {
  public static void main(String[] args) {
    int rs_1 = math.ntosum(2,3);
    System.out.println("결과 : " + rs_1);

    int rs_2 = math.ntosum(5,10);
    System.out.println("결과 : " + rs_2);
  }
}

class math {
  static int ntosum(int a, int b) {
    int s= 0;
    for (int i = a; i <= b; i ++) {
      s += i;
    }
    return s;
  }
}
profile
https://frost-puck-b0f.notion.site/B-2610fdaef71d80c49d1bccdcb575dcb5

0개의 댓글