Kotlin companion object - common use cases

Поділитися
Вставка
  • Опубліковано 23 лип 2024
  • The companion object allows you to have properties or functions without having an instance of the class. There are no static variables in Kotlin, so this is how you achieve the same thing.
    ➤ Programming Kotlin by Venkat Subramaniam: amzn.to/33yHJ7r
    ➤ Android Cookbook: amzn.to/3IfEX6h
    ➤ Head First Kotlin: amzn.to/3I77Z7Z
    discuss.kotlinlang.org/t/what...
    Hi! I’m Rahul, a software engineer with a passion for teaching. I went to Stanford, was a founding engineer at a startup, and have worked at various companies across Silicon Valley. I’m currently an Android Developer at Facebook and teach an Android class at Stanford. Would love to connect with you! This channel is about tech (especially Silicon Valley), combining tactical how-to guides with interviews.
    📹 UA-cam: ua-cam.com/users/RahulPandey...
    📝 LinkedIn: / rpandey1234
    🐦 Twitter: / rpandey1234
    📸 Instagram: / rpandey1234
    📂 Github: github.com/rpandey1234/

КОМЕНТАРІ • 41

  • @hcrnn7518
    @hcrnn7518 Рік тому

    Without a doubt, you are the most intelligent coding couch I have come across. Concise just like Kotlin. Lol

  • @shivamsethi3829
    @shivamsethi3829 3 роки тому +1

    Great explanation.

  • @noahancolie4347
    @noahancolie4347 Рік тому

    Thank you. Very clear explanation ! :)

  • @joeskymalka836
    @joeskymalka836 2 роки тому

    very professionally. excellent

  • @hossein6210
    @hossein6210 Рік тому

    tres bien et tres complet, merci beaucoup❤🙏

  • @-smileingbread-754
    @-smileingbread-754 3 роки тому +1

    A very nice video. Thank you very much.

  • @anmolmishra1914
    @anmolmishra1914 3 роки тому

    Thanks, good one

  • @bjugdbjk
    @bjugdbjk 3 роки тому +2

    Thanks a lot Rahul for sharing ur knowledge, Ur teaching skills r amazing... I will b glad if u make many more vids n kotlin concepts like flow, Stateflow,shared flow etc

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому +1

      thank you!! I have a few more Kotlin videos planned - in fact one coming in the morning :)

    • @bjugdbjk
      @bjugdbjk 3 роки тому +1

      @@RahulPandeyrkp I just happened to see ur profile man.... good to know that u r professor at Stanford nd wrkng at facebook...wow...super cool !!! Do u have any video if not could u share ur experience of being an android Engineer at Facebook nd being a teacher at the same time ...it will b super helpful ..:)

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому +2

      @@bjugdbjk yes I am actually planning to talk more about my journey in the tech industry!

  • @sujeetarya3478
    @sujeetarya3478 3 роки тому

    Exactly what I was looking for thnx

  • @DovWasserman00
    @DovWasserman00 2 роки тому

    Very nice tutorial, thank you. I would like to learn about other use cases for companion objects other than these two, which both boil down to: substitute for missing *static* keywords.
    What about the methods you can define on the object which grant access to internal class state? Or even the extension methods users can define for themselves? Is it comparable to C++ *friend* keyword? Thanks again.

  • @zeusalmighty6740
    @zeusalmighty6740 3 роки тому +1

    Is there any difference in declaring TAG as const val at file level rather than in companion object? Which do you prefer?

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому +1

      Great question- I most often see TAG declared in the companion object (that's what I do here), but there's actually no reason to do that. It's perfectly acceptable to declare a private const val variable at the file level, and that's generally more natural.

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому

      More reading: stackoverflow.com/questions/49969319/kotlin-difference-between-constant-in-companion-object-and-top-level

    • @zeusalmighty6740
      @zeusalmighty6740 3 роки тому

      @@RahulPandeyrkp Just checked the byte-code for both of them, both methods lead to encapsulating the constant in public final class. I don't see any differences here. Since TAG is closely related to the class, I think it would be better if it is coupled with the class inside of the companion object of the class. Thanks for the reply. Videos are great!

  • @AbhinavKulshreshtha
    @AbhinavKulshreshtha 3 роки тому

    In this example, does BankAccount have different objects (in memory location) but share TAG statically from same location?

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому +1

      yep, the properties defined in the companion object will be static, so there will only be one copy of them in memory, shared across all class instances.

    • @AbhinavKulshreshtha
      @AbhinavKulshreshtha 3 роки тому +1

      @@RahulPandeyrkp Thanks, I should use it more often. I recently started porting a spring application in kotlin.

  • @sahilverma4077
    @sahilverma4077 3 роки тому +1

    Dude do you have plans on making a full-length Kotlin tutorial course,
    I wanted to know cause I have many juniors who want to learn android development but I can't suggest them java cause they think it's tough so I was thinking that if you can make a full-length Kotlin series then I can share it with them, I know there are may on youtube but since I don't know their way of teaching so I can't just suggest some random tutorial to my juniors, but I have been a subscriber of this channel so I won't hesitate in recommending your video.
    Either ways it's your wish.
    Have a nice day.

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому +1

      Hi Sahil, thanks for your comment. I currently don't have a full Kotlin course planned - I've been going through the language and explaining things that I find interesting. I'll keep your idea in mind, though!

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому +1

      In the meantime, this Kotlin series by Coding in Flow is really good: ua-cam.com/play/PLrnPJCHvNZuAIbejjZA1kGfLeA8ZpICB2.html

    • @sahilverma4077
      @sahilverma4077 3 роки тому

      @@RahulPandeyrkp thanks, will send them this one

  • @LiranBarsisa
    @LiranBarsisa 3 роки тому

    I still don't understand 2 things:
    1. Is the "companion object " code supposed to be (by what's the standard) at the bottom or at the top?
    2. I've noticed I can put constants and functions outside of the class. How come? What are they? How can they be reached, via both Kotlin and Java (from outside the file)?

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому

      1. This is a style question, in my experience I've mostly seen it at the top of the class (similar to what I'd do in Java). But you have a good point in that the official Kotlin docs say to put it at the bottom: kotlinlang.org/docs/coding-conventions.html#class-layout

    • @LiranBarsisa
      @LiranBarsisa 3 роки тому

      @@RahulPandeyrkp The weird thing is that I think it sometimes get to be at the top. I think it's when you convert from Java to Kotlin.
      Do you know about the second point? It looks very similar to companion-object, but it's not. It's... something weird. Like an orphaned place.

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому

      2. Yes, variables/functions outside the class are visible at the package level. More reading: stackoverflow.com/questions/49013996/why-is-possible-to-write-a-function-outside-a-class-in-kotlin

    • @LiranBarsisa
      @LiranBarsisa 3 роки тому

      @@RahulPandeyrkp So they can be reached via Java/Kotlin, outside the file? I think I tried it and I failed..

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому

      @@LiranBarsisa I wouldn't look too much into the Java to Kotlin conversion, a human may not write the code that way

  • @scottbiggs8894
    @scottbiggs8894 2 місяці тому

    Too bad there's no way to simply define a constant without coupling it with all the overhead of a class. Or even better, simply have macros like C (with no overhead at all).

  • @KuldeepRaoMaratha
    @KuldeepRaoMaratha 3 роки тому +1

    आपकी अमरीकी अंग्रेजी हमे बिलकुल समझ नहीं आती हो सके तो विडियो हिन्दी में भी बनाओ

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 роки тому +1

      there's an option to turn on subtitles, you can try that

  • @ChrisAthanas
    @ChrisAthanas 2 роки тому

    Consider moving the mic away from your mouth or using a screen to reduce the annoying mouth noises you are making

    • @Abelmengesha-sp6lv
      @Abelmengesha-sp6lv Рік тому

      STFU! You have other million videos to watch if you're annoyed.

  • @kutilkol
    @kutilkol Рік тому

    so companion object is static in java?