Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Static import๋ฌธ์„ ์ด์šฉํ•œ ์ฝ”๋“œ ๊ฐ„๊ฒฐํ™”


class Main{
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
    }
}

๋จผ์ €, import ๋ฌธ์˜ ๊ตฌ์„ฑ์€

import java.ํŒจํ‚ค์ง€๋ช….ํด๋ž˜์Šค๋ช….๋ฉ”์„œ๋“œ๋ช…; ์œผ๋กœ ์„ ์–ธ์„ ํ•œ๋‹ค.

์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ,

System.out.println ์˜ ์‚ฌ์šฉ์ด ๋งค์šฐ ๋นˆ๋ฒˆํ•œ๋ฐ, ์ด๋ฅผ static import ๋ฌธ์„ ํ†ตํ•ด ๊ฐ„๊ฒฐํ™” ์‹œ์ผœ์ค„ ์ˆ˜ ์žˆ๋‹ค.

import static java.ํŒจํ‚ค์ง€๋ช….ํด๋ž˜์Šค๋ช….*; ์„ ์–ธ์„ ํ•˜๋ฉด,

ํŠน์ • ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

import static java.lang.System.*; //static import ๋ฌธ
class Main{
    public static void main(String[] args) {
        out.println("Hello World");
        out.println("Hello World");
        out.println("Hello World");
        out.println("Hello World");
        out.println("Hello World");
        out.println("Hello World"); // ์›๋ž˜๋Š” System.out์ด์ง€๋งŒ, System ์ƒ๋žต ๊ฐ€๋Šฅ
    }
}

์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๋ณต์žกํ•ด์ง€๊ฑฐ๋‚˜ ๊ฐ„๊ฒฐํ™”๊ฐ€ ํ•„์š”ํ•  ๊ฒฝ์šฐ์— static import ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค.