
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๋ฅผ ์ฌ์ฉํ๋ฉด ํด๋์ค ์ด๋ฆ์ ์๋ตํ๊ณ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค.
• ๋จ์ฉํ๋ฉด ์ฝ๋์ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง ์ ์์ผ๋ ์ฃผ์ํด์ผ ํ๋ค.