πͺ½
"νλ¦° λ΄μ©μ΄ μμ μ μμ΅λλ€."
βΊ λ³μ
λ³μλ, νλ‘κ·Έλλ°μμ κ°μ μ μ₯νκ³ κ΄λ¦¬νκΈ° μν΄ μ¬μ©νλ μ΄λ¦μ΄ λΆμ λ©λͺ¨λ¦¬ 곡κ°μ΄λ€.
λ³μλ λ³μκ° μ μΈλ μμΉμ λ°λΌ λ³μμ μ’ λ₯λ₯Ό μ νλ€.
1. ν΄λμ€ λ³μ (μ μλ³μ)
ν΄λμ€ λ³μμ νΉμ§μ ν¬κ² λ€κ°μ§κ° μλ€.
- λ³μ νμ μμ staticμ λΆμΈλ€.
- ν΄λμ€ λ³μλ λͺ¨λ μΈμ€ν΄μ€κ° 곡μ νλ€.
- λͺ¨λ μΈμ€ν΄μ€κ° 곡ν΅λ κ°μ 곡μ νλ€.
- ν΄λμ€ μ΄λ¦, ν΄λμ€ λ³μλͺ μ ν΅ν΄ μ κ·Όν μ μλ€.
Q. λͺ¨λ μΈμ€ν΄μ€κ° 곡μ λλ€λ λ§μ΄ λ¬΄μ¨ μλ―ΈμΈκ°?
A. μμ±λ μΈμ€ν΄μ€λ ν΄λμ€ λ³μλ₯Ό μ°Έμ‘°νλ€λ κ²μ΄λ€. μ½κ² μ€λͺ νμλ©΄ ν΄λμ€ λ³μλ λ§μμ λμΈ μ°μ²΄ν΅μ΄κ³ μΈμ€ν΄μ€λ μ§μ΄λΌκ³ λΉμ ν μ μλ€. μ§(μΈμ€ν΄μ€)λ μ°μ²΄ν΅(ν΄λμ€ λ³μ)μ νΈμ§λ₯Ό κΊΌλΌμλ νΈμ§λ₯Ό λ£μ μλ μλ€. λͺ¨λ μ§μ΄ λκ°μ μ°μ²΄ν΅μ μ°κΈ° λλ¬Έμ μ΄λ€ μ§μμ λ£μ νΈμ§λ₯Ό λ€λ₯Έ μ§μμ κΊΌλ΄ λ³Ό μ μλ€.
// chat-GPTλ₯Ό ν΅ν΄ μμ λ₯Ό λ§λ€μμ΅λλ€.
class Counter {
// staticμ΄ λΆμ κ²μ ν΅ν΄ ν΄λμ€ λ³μλΌλ κ²μ μ μ μλ€.
// ν΄λμ€ λ³μλ₯Ό 0μΌλ‘ μ΄κΈ°ννλ€.
static int count = 0;
// μμ±μ Counterλ₯Ό ν΅ν΄ new Counter()κ° μμΌλ©΄ count λ³μκ° μ¦κ°νλλ‘ νλ€.
Counter() {
count++;
}
// νμ¬ countκ° μΌλ§λ μ¦κ°νλμ§ λ³΄μ¬μ£Όλ λ©μλλ₯Ό λ§λ€μλ€.
void showCount() {
System.out.println("Count: " + count);
}
}
public class Main {
public static void main(String[] args) {
// μΈμ€ν΄μ€ c1κ³Ό c2κ° μμ±λμλ€.
// μμ±μλ₯Ό ν΅ν΄ count++κ° μ€νλλ€.
// c1κ³Ό c2λ‘ count++κ° μ΄ λ λ² μ€νλμλ€.
Counter c1 = new Counter();
Counter c2 = new Counter();
// μ΄ c1κ³Ό c2λ showCount λ©μλλ₯Ό ν΅ν΄ κ²°κ³Όκ°μ΄ μΆλ ₯λλ€.
// κ²°κ³Όκ°μ 2κ° λλ€.
// 2κ° λμ€λ μ΄μ λ λμΌν ν΄λμ€ λ³μ countλ₯Ό μΆλ ₯νκΈ° λλ¬Έμ΄λ€.
c1.showCount();
c2.showCount();
}
}
2. μΈμ€ν΄μ€ λ³μ (μ μλ³μ)
μΈμ€ν΄μ€ λ³μμ νΉμ§μ ν¬κ² μΈκ°μ§κ° μλ€.
- μΈμ€ν΄μ€κ° μμ±λμ΄μΌ λ³μκ° μμ±λλ€.
- ν΄λμ€ κ°μ²΄μ λ€λ₯΄κ² 곡μ λμ§ μλλ€.
- μΈμ€ν΄μ€λ§λ€ κ³ μ ν λ€λ₯Έ κ°μ κ°μ§ μ μλ€.
μΈμ€ν΄μ€ λ³μμ ν΄λμ€ λ³μμ μ°¨μ΄λ₯Ό μμ보λ μμ λ₯Ό μ°Ύμλ€.
// μΆμ² - sujinhopeλ
public class Main {
public static class StaticTest {
// ν΄λμ€ λ³μ
static int classVar = 10;
//μΈμ€ν΄μ€ λ³μ
int instanceVar = 28;
}
public static void main(String[] args) {
// staticTest1κ³Ό staticTest2μΈ μΈμ€ν΄μ€κ° μμ±νλ€.
StaticTest staticTest1 = new StaticTest();
StaticTest staticTest2 = new StaticTest();
// μ€ν κ°μ 1λ²κ³Ό 2λ² λͺ¨λ 10κ³Ό 28μ΄ λλ€.
System.out.println("1λ² " + staticTest1.classVar + ", " + staticTest1.instanceVar);
System.out.println("2λ² " + staticTest2.classVar + ", " + staticTest2.instanceVar);
// ν΄λμ€ λ³μ staticTest1μ κ°μ 12λ‘ λ³κ²½νλ€.
staticTest1.classVar = 12;
// μΈμ€ν΄μ€ λ³μ staticTest1μ κ°μ 400μΌλ‘ λ³κ²½νλ€.
staticTest1.instanceVar = 400;
System.out.println();
// 1λ²μ μΆλ ₯κ°μ 12μ 400μ΄ λλ€.
// 2λ²μ μΆλ ₯κ°μ 12μ 28μ΄ λλ€.
System.out.println("1λ² " + staticTest1.classVar + ", " + staticTest1.instanceVar);
System.out.println("2λ² " + staticTest2.classVar + ", " + staticTest2.instanceVar);;
}
}
ν΄λμ€ λ³μλ μΈμ€ν΄μ€μ λͺ¨λ κ°μ 곡μ νκΈ° λλ¬Έμ νλμ μΈμ€ν΄μ€ κ°μ΄ λ°λλ©΄ λͺ¨λ μΈμ€ν΄μ€κ° κ°μ λ³κ²½λλ€.
μΈμ€ν΄μ€ λ³μλ μΈμ€ν΄μ€μ κ°μ 곡μ νμ§ μκΈ° λλ¬Έμ μΈμ€ν΄μ€κ° κ°λ³μ μΌλ‘ κ°μ μ μ§νλ κ²μ΄ κ°λ₯νλ€.
Q. ν΄λμ€, μΈμ€ν΄μ€, ν΄λμ€ λ³μ, μΈμ€ν΄μ€ λ³μκ° λ¬΄μμΈκ°?
A. λΉμ λ‘ μλ₯Ό λ€μ΄λ³΄κ² λ€. [ ν΄λμ€λ λΉ΅ν , μΈμ€ν΄μ€λ λΉ΅νλ‘ μ°μ΄λΈ λΉ΅ , ν΄λμ€ λ³μλ λΉ΅μ νμμ λ¨κΈ°λ λΉ΅μ§μ μκ·Έλμ²(?) , μΈμ€ν΄μ€ λ³μλ λΉ΅ μμ λ€μ΄κ°λ μΌ ]μΌλ‘ μκ°ν μ μλ€.
3. μ§μλ³μ
- λ©μλ λ΄μμ μ μΈλλ€.
- λ©μλκ° μ€νλ λ λ©λͺ¨λ¦¬λ₯Ό ν λΉλ°λλ€.
- λ©μλκ° μ’ λ£λλ©΄ μλ©Έλμ΄ μ¬λΌμ§λ€.
// μΆμ² - sujinhopeλ
public class Main {
public static class StaticTest {
// μ μλ³μ μ€ νλμΈ ν΄λμ€ λ³μμ΄λ€.
static int classVar = 10;
// μ μλ³μ μ€ νλμΈ μΈμ€ν΄μ€ λ³μμ΄λ€.
int instanceVar = 28;
// λ©μλμΈ methodλ₯Ό λ§λ€μλ€.
public void method() {
// μ§μλ³μ localVar
int localVar = -100;
System.out.println("μ§μ λ³μ κ° : " + localVar);
}
}
public static void main(String[] args) {
// localTest μΈμ€ν΄μ€λ₯Ό λ§λ€μλ€.
StaticTest localTest = new StaticTest();
// μΈμ€ν΄μ€λ₯Ό ν΅ν΄ method μ€ννλ€.
localTest.method();
}
}
νΌμ 곡λΆνλ©΄μ μ΄ν΄νκ³ μ΄ λ΄μ©μ΄λΌ νλ¦° λ΄μ©μ΄ μμ μ μμ΅λλ€.
λκΈλ‘ μ μ΄λμλ©΄ μμ νλλ‘ νκ² μ΅λλ€. π
[ λΈλ‘κ·Έμ μ΄ λ΄μ©λ€μ μΆμ²λ μλμ μ μ΄λν λ
λ€μ΄κ°μ μ½μ΄λ³΄μλ©΄ λμ± λμμ΄ λμ€ λ― ν©λλ€ ]
< μ΄ν ν¬μ€ν - μΈμ€ν΄μ€, μμ±μ, λ§€κ°λ³μμ μΈμμ μ°¨μ΄>
κΈ°λ³Έ μ©μ΄ μ 리 - π§ μΈμ€ν΄μ€, μμ±μ κ·Έλ¦¬κ³ λ§€κ°λ³μ(Parameter)μ μΈμ(Argument)μ μ°¨μ΄
κΈ°λ³Έ μ©μ΄ μ 리 - π§ λ³μλ?πͺ½"νλ¦° λ΄μ©μ΄ μμ μ μμ΅λλ€." βΊ λ³μλ³μλ, νλ‘κ·Έλλ°μμ κ°μ μ μ₯νκ³ κ΄λ¦¬νκΈ° μν΄ μ¬μ©νλ μ΄λ¦μ΄ λΆμ λ©λͺ¨λ¦¬ 곡κ°μ΄λ€.λ³μλ λ³μκ° μ μΈ
post-this.tistory.com
- λ³μμ κ°λ μ 리 μΆμ²
[JAVA] μλ°μ λ³μ (ν΄λμ€ λ³μ, μΈμ€ν΄μ€ λ³μ, μ§μ λ³μ)
μ΄ κΈμ PC λ²μ TISTORYμ μ΅μ ν λμ΄μμ΅λλ€. μ μΈ μμΉμ λ°λ₯Έ λ³μμ μ’ λ₯ μλ°μμμ λ³μλ ν΄λμ€ λ³μ, μΈμ€ν΄μ€ λ³μ, μ§μ λ³μκ° μμ΅λλ€. λ³μμ μ’ λ₯λ₯Ό κ²°μ μ§λ κ²μ 'λ³μκ° μ μΈ
itmining.tistory.com
- sujinhopeλμ μμ μΆμ²
[java] ν΄λμ€λ³μ, μΈμ€ν΄μ€ λ³μ μ°¨μ΄(staticλ³μμ non Staticλ³μ) - Break Out of Your Comfort Zone
λͺ©μ°¨ ν΄λμ€ λ³μ, μΈμ€ν΄μ€ λ³μ ν΄λμ€ λ³μ, μΈμ€ν΄μ€ λ³μ, μ§μ λ³μκ° λ©λͺ¨λ¦¬μ μ μ¬λλ μμΉ ν΄λμ€ λ³μ(Static)μ μ£Όμμ , ν΄λμ€ λ³μκ° νμν μ΄μ ν΄λμ€ λ³μ, μΈμ€ν΄μ€ λ³μ
sujinhope.github.io
- μ μλ³μμ μ§μλ³μμ κ°λ μ 리 μΆμ²
ꡬλ¦EDU - λͺ¨λλ₯Ό μν λ§μΆ€ν ITκ΅μ‘
ꡬλ¦EDUλ λͺ¨λλ₯Ό μν λ§μΆ€ν ITκ΅μ‘ νλ«νΌμ λλ€. κ°μΈ/νκ΅/κΈ°μ λ° κΈ°κ΄ λ³ μ΅μ νλ ITκ΅μ‘ μ루μ μ κ²½νν΄λ³΄μΈμ. κΈ°μ΄λΆν° μ€λ¬΄ νλ‘κ·Έλλ° κ΅μ‘, μ κ΅ μ΄μ€κ³ /λνκ΅ μ¨λΌμΈ μμ , κΈ°μ /
edu.goorm.io
- chatGPTμ λμλ λ°μμ΅λλ€.