ν΄λμ€(Class)λ κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°(OOP)μμ μ¬μ©λλ ν΅μ¬ κ°λ μ€ νλλ‘, λΉμ·ν νΉμ±μ κ°μ§ κ°μ²΄λ€μ λ¬Άμ΄μ μΆμνν μ€κ³ λꡬμ λλ€. ν΄λμ€λ κ°μ²΄μ μνλ₯Ό λνλ΄λ μμ±(λ©€λ² λ³μ)κ³Ό κ°μ²΄κ° μνν μ μλ λμμ μ μν λ©μλ(λ©€λ² ν¨μ)λ‘ κ΅¬μ±λ©λλ€.
μ¦, ν΄λμ€λ μΈμ€ν΄μ€(κ°μ²΄)λ₯Ό μμ±νκΈ° μν μ€κ³λμ κ°μ΅λλ€. μλ₯Ό λ€μ΄, μννΈ μ€κ³λμλ μ°½λ¬Έμ΄ μ΄λ€ λ°©ν₯μ μλμ§ μ°½λ¬Έ λͺ¨μμ μ΄λ»κ² ν μ§μ λν μ λ³΄κ° μ€κ³λ(ν΄λμ€)μ μμ±λλ κ²μ λλ€. κ·Έλ¦¬κ³ μ€κ³λμ μ§μμ¬νμ λ°λΌμ κ°μ λͺ¨μμ μννΈκ° λ§λ€μ΄μ§λ μ리μ λλ€.
νλ(Fields)
ν΄λμ€ μμ νλ(Fields)λ ν΄λμ€μ λ©€λ² λ³μλ‘, κ°μ²΄μ μνλ₯Ό μ μ₯νλ μν μ ν©λλ€. νλλ ν΄λμ€ λ΄λΆμ μ μΈλλ©°, κ°μ²΄μ μμ±μ΄λ λ°μ΄ν°λ₯Ό λνλ λλ€. ν΄λμ€μ κ° μΈμ€ν΄μ€(κ°μ²΄)λ§λ€ λ³λμ νλ κ°μ κ°μ§ μ μμ΅λλ€. νλλ ν΄λμ€μ μνλ₯Ό νννκ³ , μμ±μλ₯Ό ν΅ν΄ μ΄κΈ°νλμ΄ κ°μ²΄μ μμ±κ³Ό ν¨κ» μ¬μ©λ©λλ€. νλλ ν΄λΉ ν΄λμ€μ λͺ¨λ λ©μλμμ μ¬μ©ν μ μμΌλ©°, κ°μ²΄μ νΉμ μνλ₯Ό μ μ₯νλ μ€μν μν μ ν©λλ€.
public static void main(String[] args) {
TestClass test1 = new TestClass();
TestClass test2 = new TestClass();
System.out.println(test1.classVar + ", " + test2.classVar + ", "
+ test1.instanceVar + ", " + test2.instanceVar);
test1.classVar = 12;
test1.instanceVar = 100;
System.out.println(test1.classVar + ", " + test2.classVar + ", "
+ test1.instanceVar + ", " + test2.instanceVar);
}
}
class TestClass {
static int classVar = 10; // Declared only once
int instanceVar = 28; // initialized every instance create
// local variable - only used inside method
public void localMethod() { // Declared and maintained only inside method
int localVar = 100; // No automatically initialized
}
}
λ©€λ² λ³μμ 3κ°μ§ μ’ λ₯
1) μΈμ€ν΄μ€ λ³μ (Instance Variables)
- μΈμ€ν΄μ€ λ³μλ ν΄λμ€μ μΈμ€ν΄μ€(κ°μ²΄)λ§λ€ λ 립μ μΌλ‘ μ‘΄μ¬νλ©°, κ° κ°μ²΄μ κ³ μ ν μμ±μ λνλ λλ€.
- ν΄λμ€ λ΄λΆμμ λ©μλ μΈλΆμμ μ μΈλλ©°, λ©μλλ λΈλ‘ λ΄μμ μ μΈλλ κ²½μ°μλ μ§μ λ³μκ° λ©λλ€.
- κ°μ²΄μ μμ±κ³Ό ν¨κ» μ΄κΈ°νλλ©°, κ°μ²΄κ° μλ©Έν λκΉμ§ μ μ§λ©λλ€.
- μΈμ€ν΄μ€ λ³μλ κ°μ²΄μ μνλ₯Ό νννκ³ κ°μ²΄ κ°μ λ°μ΄ν° 곡μ κ° μλλΌ κ°λ³ κ°μ²΄μ μνλ λ°μ΄ν°λ₯Ό μ μ₯ν©λλ€.
2) ν΄λμ€ λ³μ (Class Variables)
- ν΄λμ€ λ³μλ ν΄λΉ ν΄λμ€μ λͺ¨λ μΈμ€ν΄μ€λ€μ΄ 곡μ νλ λ³μλ‘, ν λ² μ μΈλλ©΄ λͺ¨λ μΈμ€ν΄μ€κ° λμΌν κ°μ 곡μ ν©λλ€.
- static ν€μλλ₯Ό μ¬μ©νμ¬ μ μΈλλ©°, ν΄λμ€ λ΄λΆ λλ λ©μλ λ΄λΆμ μ μΈλ μ μμ΅λλ€.
- κ°μ²΄μ μμ±κ³Όλ κ΄κ³μμ΄ ν΄λμ€κ° λ‘λλ λ μ΄κΈ°νλκ³ , νλ‘κ·Έλ¨μ΄ μ’ λ£λ λκΉμ§ μ‘΄μ¬ν©λλ€.
- μ£Όλ‘ μμλ κ³΅μ© λ°μ΄ν°μ κ°μ΄ μ¬λ¬ κ°μ²΄μμ 곡μ ν΄μΌ ν κ°μ μ μ₯νλ λ° μ¬μ©λ©λλ€.
3) μ§μ λ³μ(Local Variables)
λ©μλ, μμ±μ, λΈλ‘ λ±μ νΉμ μ§μ(scope)μμλ§ μ¬μ©λλ λ³μμ λλ€. μ§μ λ³μλ ν΄λΉ μ§μμμλ§ μ ν¨νλ©°, λ©μλκ° μ€νλ λ μμ±λμ΄ λ©μλκ° μ’ λ£λλ©΄ μλ©Έν©λλ€.
ν΄λμ€ λ³μλ λͺ¨λ μΈμ€ν΄μ€κ° 곡μ νλ―λ‘ μΈμ€ν΄μ€ κ°μ λ°μ΄ν°λ₯Ό 곡μ ν λ μ μ©νκ² μ¬μ©λ©λλ€. λ°λ©΄μ μΈμ€ν΄μ€ λ³μλ κ°λ³ κ°μ²΄μ μνλ₯Ό λνλ΄λ©°, κ°μ²΄ κ°μ λ 립μ μΌλ‘ μ μ§λμ΄μΌ ν λ μ¬μ©λ©λλ€. λ§μ§λ§μΌλ‘ μ§μ λ³μλ λ©μλ λ΄μμ κ°λ¨ν κ³μ°μ΄λ μμμ μΈ λ°μ΄ν° μ μ₯ λ±μ μ©λλ‘ μ¬μ©λλ©°, λ©μλ μ€νμ΄ λλλ©΄ μλ©Έλλ―λ‘ λ©λͺ¨λ¦¬ κ΄λ¦¬ μΈ‘λ©΄μμ ν¨κ³Όμ μ λλ€.
A class is a blueprint for the object. We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object. Since many houses can be made from the same description, we can create many objects from a class.
A field refers to a variable that is declared within a class. They define the characteristics and properties of objects.
Instance variables are declared in a class outside any method, constructor or block. As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
Static variables are created when the program starts and destroyed when the program stops.
'Programming > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
13 Method (0) | 2024.01.20 |
---|---|
12 Constructor (0) | 2024.01.19 |
10 Object-Oriented Programming (feat. Procedural Programming) (1) | 2024.01.19 |
9 Two-dimensional Arrays (Feat. Object Array) (0) | 2024.01.19 |
8 Array (λ°°μ΄) (0) | 2024.01.19 |
λκΈ