πŸ’»

Java ν”„λ‘œκ·Έλž˜λ° - μƒμ„±μž 쀑볡(Overloading), λ‹€λ₯Έ μƒμ„±μž 호좜(this()), μ§€μ •μ˜ˆμ•½μ–΄(static/ final/ static final) λ³Έλ¬Έ

KITRI/JAVA

Java ν”„λ‘œκ·Έλž˜λ° - μƒμ„±μž 쀑볡(Overloading), λ‹€λ₯Έ μƒμ„±μž 호좜(this()), μ§€μ •μ˜ˆμ•½μ–΄(static/ final/ static final)

λ˜νš¨λ‹ˆ 2020. 5. 6. 16:33
  • μƒμ„±μž 쀑볡

: λ§€κ°œλ³€μˆ˜λ₯Ό λ‹¬λ¦¬ν•˜λŠ” μƒμ„±μžλ₯Ό μ—¬λŸ¬ 개 μ„ μ–Έν•˜λŠ” 것

 

λ§€κ°œλ³€μˆ˜μ˜ νƒ€μž…κ³Ό 개수 그리고 μ„ μ–Έλœ μˆœμ„œκ°€ λ˜‘κ°™μ„ 경우 λ§€κ°œλ³€μˆ˜ μ΄λ¦„λ§Œ λ°”κΎΌλŠ” 것은 μƒμ„±μž μ˜€λ²„λ‘œλ”©μ΄ μ•„λ‹ˆλ‹€. 

 

 

 

  • λ‹€λ₯Έ μƒμ„±μž ν˜ΈμΆœ(this())

: μƒμ„±μžμ—μ„œ λ‹€λ₯Έ μƒμ„±μžλ₯Ό ν˜ΈμΆœν• λ•ŒλŠ” this() λ₯Ό μ‚¬μš©ν•œλ‹€. 

 

μƒμ„±μž μ˜€λ²„λ‘œλ”©μ΄ λ§Žμ•„μ§ˆ 경우 μƒμ„±μž κ°„μ˜ μ€‘λ³΅λœ μ½”λ“œκ°€ λ°œμƒν•  수 μžˆλ‹€. 맀개 λ³€μˆ˜μ˜ 수만 λ‹¬λ¦¬ν•˜κ³  ν•„λ“œ μ΄ˆκΈ°ν™” λ‚΄μš©μ΄ λΉ„μŠ·ν•œ μƒμ„±μžμ—μ„œ μ΄λŸ¬ν•œ ν˜„μƒμ„ λ³Ό 수 μžˆλ‹€. 이 κ²½μš°μ—λŠ” ν•„λ“œ μ΄ˆκΈ°ν™” λ‚΄μš©μ€ ν•œ μƒμ„±μžμ—λ§Œ μ§‘μ€‘μ μœΌλ‘œ μž‘μ„±ν•˜κ³  λ‚˜λ¨Έμ§€ μƒμ„±μžλŠ” μ΄ˆκΈ°ν™” λ‚΄μš©μ„ 가지고 μžˆλŠ” μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜λŠ” λ°©λ²•μœΌλ‘œ κ°œμ„ ν•  수 μžˆλ‹€. μ€‘볡 μ½”λ“œλ₯Ό μ΅œμ†Œν™”ν•œλ‹€. 

 

쑰건 1) λ°˜λ“œμ‹œ μƒμ„±μžμ˜ 첫 μ€„μ—μ„œλ§Œ ν—ˆμš©λœλ‹€. 2) μƒμ„±μžμ˜ μ΄λ¦„μœΌλ‘œ ν΄λž˜μŠ€μ΄λ¦„ λŒ€μ‹  thisλ₯Ό μ‚¬μš©ν•œλ‹€.

 

 

  • static

staticν•¨μˆ˜λŠ” static만 κ°€μ Έλ‹€ μ“΄λ‹€. static ν•¨μˆ˜λŠ” λ³€μˆ˜μ΄λ˜ ν•¨μˆ˜μ΄λ˜ μ™ΈλΆ€μ—μ„œ κ°€μ Έμ˜€λŠ” 건 무쑰건 static만!

setCount()ν•¨μˆ˜κ°€ λͺ‡λ²ˆ ν˜ΈμΆœλ˜μ—ˆλŠ”μ§€ μ•Œκ³  싢은데, newν‚€μ›Œλ“œλ‘œ 객체가 μƒμ„±λ λ•Œλ§ˆλ‹€ μ΄ˆκΈ°ν™”λ˜κΈ° λ•Œλ¬Έμ— 1이 좜λ ₯.

 

static λ³€μˆ˜λ‘œ μ„ μ–Έν•΄μ„œ 1, 2, 3이 좜λ ₯λ˜λŠ” 것을 λ³Ό 수 μžˆλ‹€.

static은 ν”„λ‘œκ·Έλž¨μ΄ μ’…λ£Œλ  λ•ŒκΉŒμ§€ μ‚΄μ•„μžˆλ‹€. λ‚¨μš©ν•˜λ©΄ λ©”λͺ¨λ¦¬κ³΅κ°„을 많이 μ°¨μ§€ν•˜κ²Œλ˜λ―€λ‘œ μ£Όμ˜ν•΄μ•Όν•œλ‹€. 

 

 

 

μ ‘κ·Όμ œν•œ(private, public) / μ§€μ •μ˜ˆμ•½μ–΄(static, final, static final)

μ§€μ •μ˜ˆμ•½μ–΄λŠ” (default(μ—†λŠ”κ±°), static, final, static final)둜 4κ°€μ§€λ‘œ κ΅¬μ„±ν•œλ‹€. 

 

 

staticν•¨μˆ˜λŠ” staticλ³€μˆ˜λ§Œ μ‚¬μš©κ°€λŠ₯. 클래슀λ₯Ό μ΄μš©ν•΄μ„œ μ ‘κ·Όν•΄μ•Όν•œλ‹€. (ν•¨μˆ˜ν˜ΈμΆœμ€ 클래슀λͺ…μœΌλ‘œ μ ‘κ·Όν•œλ‹€.

 

 

<ν΄λž˜μŠ€λ‹€μ΄μ–΄κ·Έλž¨(Class Diagram)>

- public : +

- private : -

- (ν•¨μˆ˜λͺ…)(λ³€μˆ˜λͺ… : μžλ£Œν˜•)

- static : 밑쀄(ν•¨μˆ˜ λ³€μˆ˜ λ‘˜λ‹€ μ‚¬μš©)

 

 

 

 

 


API λ¬Έμ„œ 자주 보기!!

 

곡식 λ¬Έμ„œ 잘 μ°Ύμ•„μ„œ μ“Έ 쀄 μ•Œμ•„μ•Όν•œλ‹€.

 

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/io/PrintStream.html

 

PrintStream (Java SE 14 & JDK 14)

All Implemented Interfaces: Closeable, Flushable, Appendable, AutoCloseable Direct Known Subclasses: LogStream public class PrintStream extends FilterOutputStream implements Appendable, Closeable A PrintStream adds functionality to another output stream, n

docs.oracle.com

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Scanner.html

 

Scanner (Java SE 14 & JDK 14)

All Implemented Interfaces: Closeable, AutoCloseable, Iterator public final class Scanner extends Object implements Iterator , Closeable A simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input

docs.oracle.com


 

 

  • final

finalν•„λ“œλŠ” μ΄ˆκΈ°κ°’μ΄ μ €μž₯되면 이것이 μ΅œμ’…μ μΈ 값이 λ˜μ–΄μ„œ ν”„λ‘œκ·Έλž¨ μ‹€ν–‰ 도쀑에 μˆ˜μ •μ΄ μ•ˆλœλ‹€.

즉, κΈ°λ³Έκ°œλ…μ€ 값이 μΉ˜ν™˜ν•˜μ§€ μ•ŠλŠ”λ‹€. μ§€μ—­λ³€μˆ˜λ‘œ 선언은 κ°€λŠ₯ν•˜λ‹€. 

 

final 은 값을 받을 μˆ˜λŠ” μžˆμ§€λ§Œ, 전달해주지 λͺ»ν•œλ‹€. 

 

 

<클래슀 λ‹€μ΄μ–΄κ·Έλž¨>

final: {readOnly}

 

 

 

 

 

 

 

  • static final

μƒμˆ˜(λΆˆλ³€μ˜ κ°’) ν•œ 번 μ΄ˆκΈ°κ°’μ΄ μ €μž₯되면 λ³€κ²½ν•  수 μ—†λ‹€. 

staticν•¨μˆ˜μ—μ„œ final λ³€μˆ˜ μ‚¬μš© μ•ˆλœλ‹€. μ“Έ 수 있으렀면 static finalλ₯Ό μ‚¬μš©ν•œλ‹€.

 

 

[정리]

  • μƒμ„±μž 쀑볡은 λ§€κ°œλ³€μˆ˜μ˜ κ°œμˆ˜λ‚˜ νƒ€μž…μ΄ λ‹€λ₯΄λ©΄ μ‚¬μš©κ°€λŠ₯ν•˜λ‹€. 
  • μƒμ„±μž 간에 ν˜ΈμΆœν•  μ‹œμ—λŠ” this() λ₯Ό μ‚¬μš©ν•œλ‹€. 
  • static ν•¨μˆ˜ λ‚΄μ—μ„œλŠ” static λ³€μˆ˜λ§Œ μ“΄λ‹€. (static(o), static final(o), final(x), default(x))
  • final은 λ³€ν•˜μ§€ μ•ŠλŠ” 값일 λ•Œ μ‚¬μš©ν•œλ‹€.

 

 

 

 

static 이 μ‚¬μš©λ  수 μžˆλŠ” κ³³ - λ©€λ²„λ³€μˆ˜, λ©”μ„œλ“œ, μ΄ˆκΈ°ν™” λΈ”λŸ­

μ œμ–΄μž λŒ€μƒ 의미
static λ©€λ²„λ³€μˆ˜ - λͺ¨λ“  μΈμŠ€ν„΄μŠ€μ— κ³΅ν†΅μ μœΌλ‘œ μ‚¬μš©λ˜λŠ” ν΄λž˜μŠ€λ³€μˆ˜κ°€ λœλ‹€. 
- ν΄λž˜μŠ€λ³€μˆ˜λŠ” μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ§€ μ•Šκ³ λ„ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.
- ν΄λž˜μŠ€κ°€ λ©”λͺ¨λ¦¬μ— λ‘œλ“œλ  λ•Œ μƒμ„±λœλ‹€.
λ©”μ„œλ“œ - μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ§€ μ•Šκ³ λ„ 호좜이 κ°€λŠ₯ν•œ staticλ©”μ„œλ“œκ°€ λœλ‹€. 
- static λ©”μ„œλ“œ λ‚΄μ—μ„œλŠ” μΈμŠ€ν„΄μŠ€λ©€λ²„λ“€μ„ 직접 μ‚¬μš©ν•  수 μ—†λ‹€. 

 

final 이 μ‚¬μš©λ  수 μžˆλŠ” κ³³ - 클래슀, λ©”μ„œλ“œ, λ©€λ²„λ³€μˆ˜, μ§€μ—­λ³€μˆ˜

 

λ³€μˆ˜μ— μ‚¬μš©λ˜λ©΄ 값을 λ³€κ²½ν•  수 μ—†λŠ” μƒμˆ˜κ°€ 되며, λ©”μ†Œλ“œμ— μ‚¬μš©λ˜λ©΄ μ˜€λ²„λΌμ΄λ”©μ„ ν•  수 μ—†κ²Œ 되고 ν΄λž˜μŠ€μ— μ‚¬μš©λ˜κ²Œ 되면 μžμ‹ μ„ ν™•μž₯ν•˜λŠ” μžμ†ν΄λž˜μŠ€λ₯Ό μ •μ˜ν•˜μ§€ λͺ»ν•˜κ²Œ λœλ‹€.

μ œμ–΄μž λŒ€μƒ 의미
final 클래슀 변경될 수 μ—†λŠ” 클래슀, ν™•μž₯될 수 μ—†λŠ” ν΄λž˜μŠ€κ°€ λœλ‹€.
κ·Έλž˜μ„œ final둜 μ§€μ •λœ ν΄λž˜μŠ€λŠ” λ‹€λ₯Έ 클래슀의 쑰상이 될 수 μ—†λ‹€. 
λ©”μ„œλ“œ 변경될 수 μ—†λŠ” λ©”μ„œλ“œ, final둜 μ§€μ •λœ λ©”μ„œλ“œλŠ” μ˜€λ²„λΌμ΄λ”©μ„ 톡해 μž¬μ •μ˜ 될 수 μ—†λ‹€. 
λ©€λ²„λ³€μˆ˜  λ³€μˆ˜μ•žμ— final이 λΆ™μœΌλ©΄, 값을 λ³€κ²½ν•  수 μ—†λŠ” μƒμˆ˜κ°€ λœλ‹€. 
μ§€μ—­λ³€μˆ˜

 

λ°˜μ‘ν˜•
Comments