Variables
1. Member variable (Instance variable)
Declared in a class, but outside a method
Class
μ μΌμClass μμμ μ μΈλ λ³μ
Instance
κ° μμ±λ λ μμ±λ¨Instance λ³μμ κ°μ μ½μ΄μ€κ±°λ, μ μ₯νλ €λ©΄ Instanceλ₯Ό λ¨Όμ μμ±ν΄μΌ ν¨!
μ΄κΈ°κ°μ μ€μ νμ§ μμΌλ©΄ μλ default μ΄κΈ°νκ° λ¨
ex) String name = null; ν νμ μμ!
2. Local variable
ν¨μμ μΌμ
method(ν¨μ) μμμ μ μΈλ λ³μ
method λ΄μμλ§ μ¬μ© κ°λ₯
methodκ° μ€ν λ λ memoryλ₯Ό ν λΉ λ°μΌλ©°, λλλ©΄ μλ©Έλμ΄ μ¬μ©ν μ μλ€
default μ΄κΈ°νκ° μλ¨
ex) String name = null; ν΄μ€μΌ ν¨
3. Class variable (Static variable)
Declared with the
static keyword
in a class, but outside a method, constructor, or block
Instance variableμ
static
λ§ λΆμ΄λ©΄ λ¨μΈμ€ν΄μ€ λ³μλ κ°κ° κ³ μ ν κ°μ κ°μ§μ§λ§, ν΄λμ€ λ³μλ λͺ¨λ μΈμ€ν΄μ€κ° 곡ν΅λ κ°μ 곡μ νκ² λ¨
ν ν΄λμ€μ λͺ¨λ μΈμ€ν΄μ€λ€μ΄ 곡ν΅μ μΈ κ°μ κ°μ ΈμΌν λ ν΄λμ€ λ³μλ‘ μ μΈνλ€!
ν΄λμ€κ° λ‘λ©λ λ μμ±λμ΄(= λ©λͺ¨λ¦¬μ λ± νλ²λ§ μ¬λΌκ°) μ’ λ£ λ λ κΉμ§ μ μ§λλ€
public
μ λΆμ΄λ©΄ κ°μ νλ‘κ·Έλ¨ λ΄μμ μ΄λμλ μ κ·Όν μ μλ μ μ λ³μκ° λ¨μΈμ€ν΄μ€ λ³μμ μ κ·Όλ²κ³Ό λ€λ₯΄κ² μΈμ€ν΄μ€λ₯Ό μμ±νμ§ μκ³ ν΄λμ€μ΄λ¦ & ν΄λμ€λ³μλͺ μ ν΅ν΄μ μ κ·Ό
Null Assign
μ νλ μ΄μ (from docs.oracle)
Null Assign
μ νλ μ΄μ (from docs.oracle): Assign null to Variables That Are No Longer Needed
Explicitly assigning a null value to variables that are no longer needed helps the garbage collector to identify the parts of memory that can be safely reclaimed. Although Java provides memory management, it does not prevent memory leaks or using excessive amounts of memory.
An application may induce memory leaks by not releasing object references. Doing so prevents the Java garbage collector from reclaiming those objects, and results in increasing amounts of memory being used. Explicitly nullifying references to variables after their use allows the garbage collector to reclaim memory.
One way to detect memory leaks is to employ profiling tools and take memory snapshots after each transaction. A leak-free application in steady state will show a steady active heap memory after garbage collections.
λ€ μ°κ³ λ μμμ null assignμ ν΄μΌνλ€!
-> null assignμ νμ§ μμΌλ©΄ μμμ λ§μ΄ λ¨Ήμ΄μ μ±λ₯μ΄ μμ’μμ§κΈ° λλ¬Έ!
But, null assign νλ€κ³ λͺ¨λ μμμ΄ λ°λ©λλκ² μλλ€
-> μ΄λ κ² ν΄μΌ λ€ λ°λ© ν μ μμ
Last updated