| English | 의미 |
|---|---|
| instance method/ˈɪnstəns ˈmeθəd/ | 인스턴스 메서드 |
Calling on an object
- An instance method 实例方法 is called on a specific object, using dot notation.
word.length()callslength()on the objectword.- The object before the dot is the one the method acts on.
- Unlike a class method, you need a real object to call it.
The object is the context
- Inside an instance method, the code works with that object's state.
"cat".length()returns3;"hello".length()returns5— same method, different objects.- The method reads (or changes) the state of the object it was called on.
- Two objects of one class can give different results from the same call.
Arguments and return values
- Instance methods can take arguments and return a value, just like class methods.
"hello".substring(1, 3)passes1and3, returns"el".- Capture the returned value:
String part = "hello".substring(1, 3); - The dot's object plus the arguments together decide the result.
Instance vs. class, side by side
- Class method:
Math.sqrt(16)— dot after a class name. - Instance method:
word.length()— dot after an object. - If it needs a specific object's data, it's an instance method.
- If it's a general utility with no object, it's a class (static) method.
An instance method needs an object before the dot: word.length(), not String.length(). The object before the dot supplies the state the method acts on, so the same call gives different results on different objects. Class methods (Math.sqrt) use a class name instead — knowing which is which tells you whether you need an object first.
Same method, different objects:
String a = "cat", b = "tiger";a.length()→3(acts on"cat").b.length()→5(acts on"tiger") — the object before the dot decides.
An instance method is called on an object with dot notation (word.length()); the object before the dot supplies the state the method works with. It can take arguments and return a value like any method. This contrasts with a class (static) method, called on a class name (Math.sqrt(16)).
인스턴스 메서드는 해당 오브젝트에 대해 작동함
length()는 점(dot) 앞의 오브젝트의 상태를 읽음.
String word에 대한 인스턴스 메서드 length에 대한 올바른 호출은 무엇인가?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
인스턴스 메서드는 오브젝트에서 호출됨: object.method().
"tiger".length()의 반환값은 무엇인가?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
"tiger"는 5개의 문자를 가짐.
Math.sqrt(16)는 클래스 메서드이고 word.length()는 인스턴스 메서드다. 차이는...
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
점(dot) 앞에 클래스명 → 클래스 메서드; 오브젝트 → 인스턴스 메서드.
동일한 인스턴스 메서드 호출이라도 서로 다른 오브젝트에서는 다른 결과를 줄 수 있음.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
점(dot) 앞의 오브젝트가 상태를 제공함.
인스턴스 메서드는 구체적인 ___에 대해 dot 표기법으로 호출됨(단어 하나).
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
object.method() — 오브젝트가 컨텍스트를 제공함.