///////
Search

Static, ์˜ˆ์™ธ์ฒ˜๋ฆฌ

์ž‘์„ฑ ๋‚ ์งœ
2022/09/28 02:35
Text
Text 1
์ž‘์„ฑ์ž

Static ๋ณ€์ˆ˜

ํด๋ž˜์Šค ๋‚ด์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋“ค์ด ๊ณต์œ ํ•˜๋ฉฐ, ๋ฉ”๋ชจ๋ฆฌ์— ํ•œ๋ฒˆ ํ• ๋‹น๋˜์–ด ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋  ๋•Œ ์—†์–ด์ง„๋‹ค

ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ์ ‘๊ทผํ•˜๋Š” ๋ฒ•

1.
ํด๋ž˜์Šค ์ด๋ฆ„์„ ํ†ตํ•œ ์ ‘๊ทผ
2.
์ธ์Šคํ„ด์Šค ์ด๋ฆ„์„ ํ†ตํ•œ ์ ‘๊ทผ

๊ณต์œ ๋ณ€์ˆ˜

Instance ๋ณ€์ˆ˜๋Š” ํ•ด๋‹น instance์•ˆ์—์„œ๋งŒ ์œ ํšจํ•˜์ง€๋งŒ static ๋ณ€์ˆ˜๋Š” ํ•œ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ํด๋ž˜์Šค์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋“ค์ด ๊ณต์œ ํ•œ๋‹ค
class InstCnt{ static int instNum = 0; InstCnt(){ instNum++; } } class ClassVar{ public static void main(String[] args){ InstCnt cnt1 = new InstCnt(); InstCnt cnt2 = new InstCnt(); InstCnt cnt3 = new InstCnt(); } }
Java
๋ณต์‚ฌ
instNum์ด InstCnt object๊ฐ€ ์ƒ์„ฑ๋ ๋•Œ๋งˆ๋‹ค ์—…๋ฐ์ดํŠธ ๋˜์–ด 3์ด ๋œ๋‹ค

ํ™œ์šฉ

์ธ์Šคํ„ด์Šค ๋ณ„๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์„ ํ•„์š” ์—†์ด ๊ฐ’์˜ ์ฐฝ์กฐ๋‚˜ ๊ณต์œ ๊ฐ€ ๋ชฉ์ ์ธ ๋ณ€์ˆ˜
static final double PI = 3.14;
Java
๋ณต์‚ฌ
์™ธ๋ถ€์—์„œ๋„ ์ฐธ์กฐํ•˜๋Š” ๊ฐ’์ด๋ผ๋ฉด public์œผ๋กœ ์„ ์–ธํ•œ๋‹ค

Static Method์—๋Š” Instance ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์—†๋‹ค

class AAA{ int num = 0; static void addNum(int n) { num += n; ---> error } }
Java
๋ณต์‚ฌ
Static method๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•„๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, num ๋ณ€์ˆ˜๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ธฐ ์ „์—๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค

Static ํ•จ์ˆ˜

System.out.println();

public static void main(String[] args)

public ๋ชจ๋“  ํด๋ž˜์Šค๊ฐ€ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก
dtatic ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ๊ด€๊ณ„์—†์ด ๊ฐ€์žฅ ๋จผ์ € ํ˜ธ์ถœ ๋˜๋„๋ก

์˜ˆ์™ธ ์ฒ˜๋ฆฌ

Try and Catch

try์•ˆ์˜ ๋ช…๋ น์— ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋ฉด catch ์•ˆ์˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•œ๋‹ค
๋ฌธ๋ฒ• ์˜ค๋ฅ˜๊ฐ€ ์•„๋‹Œ runtime์— ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค
try { int x = 0/ 0; System.out.println("ํŠธ๋ผ์ด ์•ˆ"); } catch (Exception e) { System.out.println("์บ์น˜ ์•ˆ"); } finally { System.out.println("๋ฌด์กฐ๊ฑด ์‹คํ–‰"); }
Java
๋ณต์‚ฌ
output:
์บ์น˜ ์•ˆ
๋ฌด์กฐ๊ฑด ์‹คํ–‰
0/0์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด catch๋กœ ๋„˜์–ด๊ฐ„๋‹ค

Finally

try๋‚˜ catch๊ฐ€ ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ์‹คํ–‰๋˜๋”๋ผ๋„ ๋ฌด์กฐ๊ฑด ์‹คํ–‰๋œ๋‹ค

Exception

๋ชจ๋“  exception์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค

ArithmeticException

์—ฐ์‚ฐ์˜ค๋ฅ˜
ํ•˜์ง€๋งŒ ArithmaticException๊ณผ Exception์ด ์ƒ์†๊ด€๊ณ„์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Exception๋งŒ ์žˆ์–ด๋„ ์˜ค๋ฅ˜๋ฅผ ์žก์„ ์ˆ˜ ์žˆ๋‹ค.

InputMismatchException

Exception e๋กœ๋Š” int๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•  ๋•Œ string์„ ์ž…๋ ฅํ•˜๋ฉด ๋ฐœ์ƒํ•œ๋Š” ์˜ค๋ฅ˜๋ฅผ ์žก์„ ์ˆ˜ ์—†๋‹ค

Throws

JVM์—๊ฒŒ Exception์„ ๋˜์ง€๊ฒ ๋‹ค
ํ•จ์ˆ˜์˜ parameter๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฐ€๋กœ ์˜†์— ๋‹ฌ์•„์ค€๋‹ค
public static void main(String[] args) throws IOException
Java
๋ณต์‚ฌ

IOException

file์„ ์ฝ์œผ๋ ค๋ฉด ํ•„์š”ํ•˜๋‹ค

์—ฌ๋Ÿฌ๊ฐœ์˜ exception์„ throwํ•˜๊ณ  ์‹ถ์œผ๋ฉด

public void method() throws IOException, InputMismatchException {}

์˜ˆ์™ธ ํด๋ž˜์Šค ๊ตฌ๋ถ„

Checked Exception ๊ฐœ๋ฐœ์ž๊ฐ€ ์ฒ˜๋ฆฌ์•ผํ•ด ํ•˜๋Š” ์˜ค๋ฅ˜

Catch ์•ˆ ์˜ค๋ฅ˜ ๋‹ค๋ฃจ๋Š” ๋ช…๋ น์–ด:

System.out.println(e.getMessage()); ์˜ค๋ฅ˜์˜ ์ด์œ ๋ฅผ ์ถœ๋ ฅ
e.printStackTrace(); ๊ฐœ๋ฐœ์ž๊ฐ€ ์˜ค๋ฅ˜๋ฅผ trackํ•  ์ˆ˜ ์žˆ์Œ