Kotlin companion object - common use cases

Поделиться
HTML-код
  • Опубликовано: 24 ноя 2024

Комментарии • 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.

  • @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!

  • @joeskymalka836
    @joeskymalka836 2 года назад

    very professionally. excellent

  • @hossein6210
    @hossein6210 2 года назад

    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

  • @noahancolie4347
    @noahancolie4347 2 года назад

    Thank you. Very clear explanation ! :)

  • @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!

  • @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: ruclips.net/p/PLrnPJCHvNZuAIbejjZA1kGfLeA8ZpICB2

    • @sahilverma4077
      @sahilverma4077 3 года назад

      @@RahulPandeyrkp thanks, will send them this one

  • @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.

  • @scottbiggs8894
    @scottbiggs8894 6 месяцев назад

    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).

  • @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

  • @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 2 года назад

    so companion object is static in java?