λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Programming/Java

11 Class intro - feat. Field (Member variable)

by Dowon Kang 2024. 1. 19.

클래슀(Class)λŠ” κ°μ²΄μ§€ν–₯ ν”„λ‘œκ·Έλž˜λ°(OOP)μ—μ„œ μ‚¬μš©λ˜λŠ” ν•΅μ‹¬ κ°œλ… μ€‘ ν•˜λ‚˜λ‘œ, λΉ„μŠ·ν•œ νŠΉμ„±μ„ κ°€μ§„ κ°μ²΄λ“€μ„ λ¬Άμ–΄μ„œ μΆ”μƒν™”ν•œ μ„€κ³„ λ„κ΅¬μž…λ‹ˆλ‹€. ν΄λž˜μŠ€λŠ” κ°μ²΄μ˜ μƒνƒœλ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ†μ„±(멀버 λ³€μˆ˜)κ³Ό κ°μ²΄κ°€ μˆ˜ν–‰ν•  μˆ˜ μžˆλŠ” λ™μž‘을 μ •μ˜ν•œ λ©”μ„œλ“œ(멀버 ν•¨μˆ˜)둜 κ΅¬μ„±λ©λ‹ˆλ‹€.

즉, ν΄λž˜μŠ€λŠ” μΈμŠ€ν„΄μŠ€(객체)λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ 섀계도와 κ°™μŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, μ•„νŒŒνŠΈ μ„€κ³„λ„μ—λŠ” 창문이 μ–΄λ–€ λ°©ν–₯에 μžˆλŠ”μ§€ μ°½λ¬Έ λͺ¨μ–‘은 μ–΄λ–»κ²Œ ν•  지에 λŒ€ν•œ 정보가 섀계도(클래슀)에 μž‘μ„±λ˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 그리고 μ„€κ³„λ„μ˜ μ§€μ‹œμ‚¬ν•­μ— λ”°λΌμ„œ 같은 λͺ¨μ–‘μ˜ μ•„νŒŒνŠΈκ°€ λ§Œλ“€μ–΄μ§€λŠ” μ›λ¦¬μž…λ‹ˆλ‹€. 

 

 

 


 

 

ν•„λ“œ(Fields)

클래슀 μ•ˆμ˜ ν•„λ“œ(Fields)λŠ” 클래슀의 멀버 λ³€μˆ˜λ‘œ, 객체의 μƒνƒœλ₯Ό μ €μž₯ν•˜λŠ” 역할을 ν•©λ‹ˆλ‹€. ν•„λ“œλŠ” 클래슀 내뢀에 μ„ μ–Έλ˜λ©°, 객체의 μ†μ„±μ΄λ‚˜ 데이터λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. 클래슀의 각 μΈμŠ€ν„΄μŠ€(객체)λ§ˆλ‹€ λ³„λ„μ˜ ν•„λ“œ 값을 κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€. ν•„λ“œλŠ” 클래슀의 μƒνƒœλ₯Ό ν‘œν˜„ν•˜κ³ , μƒμ„±μžλ₯Ό 톡해 μ΄ˆκΈ°ν™”λ˜μ–΄ 객체의 생성과 ν•¨κ»˜ μ‚¬μš©λ©λ‹ˆλ‹€. ν•„λ“œλŠ” ν•΄λ‹Ή 클래슀의 λͺ¨λ“  λ©”μ„œλ“œμ—μ„œ μ‚¬μš©ν•  수 있으며, 객체의 νŠΉμ • μƒνƒœλ₯Ό μ €μž₯ν•˜λŠ” μ€‘μš”ν•œ 역할을 ν•©λ‹ˆλ‹€.

더보기
public class _01_Field {
    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)μ—μ„œλ§Œ μ‚¬μš©λ˜λŠ” λ³€μˆ˜μž…λ‹ˆλ‹€. 지역 λ³€μˆ˜λŠ” ν•΄λ‹Ή μ§€μ—­μ—μ„œλ§Œ μœ νš¨ν•˜λ©°, λ©”μ„œλ“œκ°€ 싀행될 λ•Œ μƒμ„±λ˜μ–΄ λ©”μ„œλ“œκ°€ μ’…λ£Œλ˜λ©΄ μ†Œλ©Έν•©λ‹ˆλ‹€.

 

 

클래슀 λ³€μˆ˜λŠ” λͺ¨λ“  μΈμŠ€ν„΄μŠ€κ°€ κ³΅μœ ν•˜λ―€λ‘œ μΈμŠ€ν„΄μŠ€ 간에 데이터λ₯Ό κ³΅μœ ν•  λ•Œ μœ μš©ν•˜κ²Œ μ‚¬μš©λ©λ‹ˆλ‹€. λ°˜λ©΄μ— μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λŠ” κ°œλ³„ 객체의 μƒνƒœλ₯Ό λ‚˜νƒ€λ‚΄λ©°, 객체 간에 λ…λ¦½μ μœΌλ‘œ μœ μ§€λ˜μ–΄μ•Ό ν•  λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€. λ§ˆμ§€λ§‰μœΌλ‘œ 지역 λ³€μˆ˜λŠ” λ©”μ„œλ“œ λ‚΄μ—μ„œ κ°„λ‹¨ν•œ κ³„μ‚°μ΄λ‚˜ μž„μ‹œμ μΈ λ°μ΄ν„° μ €μž₯ λ“±μ˜ μš©λ„λ‘œ μ‚¬μš©λ˜λ©°, λ©”μ„œλ“œ μ‹€ν–‰μ΄ λλ‚˜λ©΄ μ†Œλ©Έλ˜λ―€λ‘œ λ©”λͺ¨λ¦¬ κ΄€λ¦¬ μΈ‘λ©΄μ—μ„œ νš¨κ³Όμ μž…λ‹ˆλ‹€.

 

 

 


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.

 

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

λŒ“κΈ€