Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIL] kotlin companionn object #21

Open
yunyoung1819 opened this issue Feb 5, 2024 · 0 comments
Open

[TIL] kotlin companionn object #21

yunyoung1819 opened this issue Feb 5, 2024 · 0 comments
Assignees
Labels

Comments

@yunyoung1819
Copy link
Owner

yunyoung1819 commented Feb 5, 2024

Date

  • 2024.02.06

Title

  • companion object

Description

  • kotlin에서 companion object 는 클래스 내부에 정의 된 객체로, 해당 클래스와 관련된 정적(static) 필드와 메서드를 담기 위해 사용

주요 특징

  • 클래스 이름으로 직접 접근 가능
  • 인스턴스 메서드처럼 오버라이드 할 수 없음
  • 인터페이스 구현 가능
  • 확장 함수 정의 가능
class MyClass {
    companion object {
        val staticValue = "I am static"
        fun staticFunction() {
            println("this is static function")
        }
    }
}

fun main() {
    println(MyClass.staticValue) // "I am static" 출력
    MyClass.staticFunction() // "this is static function" 출력
}

Reference

@yunyoung1819 yunyoung1819 self-assigned this Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant