1. MathArrayUtils ํด๋ž˜์Šค ๊ตฌํ˜„

package javaBasicLecture.static2.ex;

public class MathArrayUtils {
   
   // ์ƒ์„ฑ์ž๋ฅผ private๋กœ ์„ค์ •ํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ์„ค์ • ๋ฐฉ์ง€
   private MathArrayUtils() {
        throw new UnsupportedOperationException("์ด ํด๋ž˜์Šค๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค์ด๋ฉฐ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
   }
   
   // ๋ฐฐ์—ด์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฉ”์„œ๋“œ
   public static int sum(int[] array) {
       int sum = 0;
       for (int num : array) {
            sum += num;
       }
       return sum;
   }
   
   // ๋ฐฐ์—ด์˜ ํ‰๊ท ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฉ”์„œ๋“œ
   public static double average(int[] array) {
         int min = array[0];
         for (int num : array) {
             if (num < min) {
                 min = num;
             }
         }
         return min;
   }
   // ๋ฐฐ์—ด์˜ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๋Š” ๋ฉ”์„œ๋“œ
   public static int max(int[] array) {
       int max = array[0];
       for (int num : array) {
           if (num > max) {
              max = num;
           }
       }
       return max;
   }
}

 

๊ตฌํ˜„ ์˜๋„

1. ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋ฐฉ์ง€:

  • private MathArrayUtils()๋ฅผ ํ†ตํ•ด ์™ธ๋ถ€์—์„œ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋„๋ก ๋ง‰์•˜๋‹ค.

    ์ด ๋ฐฉ์‹์€ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค์— ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.

 

2. static ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ:

   • sum, average, min, max๋Š” ๋ชจ๋‘ static์œผ๋กœ ์„ ์–ธ๋˜์—ˆ๋‹ค.

       static ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋ฐ”๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 


2. MathArrayUtilsMain ํด๋ž˜์Šค

 ์ด ํด๋ž˜์Šค๋Š” MathArrayUtils์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐฐ์—ด์˜ ํ•ฉ, ํ‰๊ท , ์ตœ์†Œ๊ฐ’, ์ตœ๋Œ€๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

package javaBasicLecture.static2.ex;

import static javaBasicLecture.static2.ex.MathArrayUtils.*; //static import ์‚ฌ์šฉ

public class MathArrayUtilsMain {
   public static void main(String[] args) {
      int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      
      // MathArrayUtils ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ
        System.out.println("sum= " + sum(values));       // static import๋กœ ํด๋ž˜์Šค๋ช… ์ƒ๋žต ๊ฐ€๋Šฅ
        System.out.println("average= " + average(values));
        System.out.println("min= " + min(values));
        System.out.println("max= " + max(values));
   }
}

 

๊ตฌํ˜„ ์˜๋„

1. static import:

    • import static javaBasicLecture.static2.ex.MathArrayUtils.*;๋ฅผ ํ†ตํ•ด ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค

       ํด๋ž˜์Šค ์ด๋ฆ„ ์—†์ด sum(values)์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

2. ์ฝ”๋“œ์˜ ๊ฐ„๊ฒฐ์„ฑ:

    • static import๋Š” ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ  ๋ถˆํ•„์š”ํ•œ ํด๋ž˜์Šค ์ด๋ฆ„ ๋ฐ˜๋ณต์„ ์ค„์ด๋Š” ๋ฐ ์œ ์šฉํ•˜๋‹ค.

 

< import static๋ž€ ๋ฌด์—‡์ธ๊ฐ€? >
import static๋Š” Java์—์„œ ์ •์ (static) ๋ฉค๋ฒ„(๋ฉ”์„œ๋“œ ๋˜๋Š” ํ•„๋“œ)๋ฅผ ํด๋ž˜์Šค ์ด๋ฆ„ ์—†์ด ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

< ์ผ๋ฐ˜์ ์ธ import์™€์˜ ์ฐจ์ด >
 ์ผ๋ฐ˜์ ์ธ import๋Š” ํด๋ž˜์Šค ์ด๋ฆ„์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•  ๋•Œ๋งˆ๋‹ค ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.
• static import๋Š” ํด๋ž˜์Šค์˜ static ๋ฉค๋ฒ„(๋ฉ”์„œ๋“œ ๋˜๋Š” ํ•„๋“œ)๋ฅผ ์ด๋ฆ„๋งŒ์œผ๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

์˜ˆ์‹œ: ์ผ๋ฐ˜ import
import java.lang.Math;

public class Example {
   public static void main(String[] args) {
       double result = Math.sqrt(16);  // ํด๋ž˜์Šค ์ด๋ฆ„(Math) ํ•„์š”
       System.out.println(result);    // ์ถœ๋ ฅ: 4.0
   }
}โ€‹

์˜ˆ์‹œ: static import
import static java.lang.Math.*;

public class Example {
    public static void main(String[] args) {
        double result = sqrt(16);      // ํด๋ž˜์Šค ์ด๋ฆ„ ์—†์ด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
        System.out.println(result);   // ์ถœ๋ ฅ: 4.0
    }
}โ€‹


< ์™œ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€? >
static import๋Š” ์ฝ”๋“œ๋ฅผ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

์ฃผ์š” ๋ชฉ์ :
1. ์ฝ”๋“œ ๊ฐ€๋…์„ฑ ํ–ฅ์ƒ: ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์“ฐ์ง€ ์•Š์•„๋„ ๋˜๋ฏ€๋กœ ์ฝ”๋“œ๊ฐ€ ์งง์•„์ง€๊ณ  ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค.
2. ํŽธ๋ฆฌํ•œ ํ˜ธ์ถœ: ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ๋‚˜ ์ƒ์ˆ˜๋ฅผ ๋” ์‰ฝ๊ฒŒ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์‹คํ–‰๊ฒฐ๊ณผ

sum= 55
average= 5.5
min= 1
max= 10

 

 

 

 ํ•™์Šต ํฌ์ธํŠธ
1. static ํ‚ค์›Œ๋“œ์˜ ์˜๋ฏธ:
    static ๋ฉ”์„œ๋“œ๋Š” ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ์—†์ด ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
     ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋Š” static์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

2. private ์ƒ์„ฑ์ž:
  • ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค์—์„œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์„ ๋ง‰๋Š” ํ‘œ์ค€ ๋ฐฉ๋ฒ•์ด๋‹ค.

3. static import ์‚ฌ์šฉ๋ฒ•:
  • static import๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํด๋ž˜์Šค ์ด๋ฆ„์„ ์ƒ๋žตํ•˜๊ณ  ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‚จ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

+ Recent posts