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 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 ..:)
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.
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 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!
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.
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!
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.
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).
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)?
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
@@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.
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
Without a doubt, you are the most intelligent coding couch I have come across. Concise just like Kotlin. Lol
Great explanation.
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
thank you!! I have a few more Kotlin videos planned - in fact one coming in the morning :)
@@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 ..:)
@@bjugdbjk yes I am actually planning to talk more about my journey in the tech industry!
very professionally. excellent
tres bien et tres complet, merci beaucoup❤🙏
A very nice video. Thank you very much.
Glad you liked it!
Thanks, good one
Thank you. Very clear explanation ! :)
Exactly what I was looking for thnx
Yaaay
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.
Is there any difference in declaring TAG as const val at file level rather than in companion object? Which do you prefer?
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.
More reading: stackoverflow.com/questions/49969319/kotlin-difference-between-constant-in-companion-object-and-top-level
@@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!
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.
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!
In the meantime, this Kotlin series by Coding in Flow is really good: ruclips.net/p/PLrnPJCHvNZuAIbejjZA1kGfLeA8ZpICB2
@@RahulPandeyrkp thanks, will send them this one
In this example, does BankAccount have different objects (in memory location) but share TAG statically from same location?
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.
@@RahulPandeyrkp Thanks, I should use it more often. I recently started porting a spring application in kotlin.
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).
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)?
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
@@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.
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
@@RahulPandeyrkp So they can be reached via Java/Kotlin, outside the file? I think I tried it and I failed..
@@LiranBarsisa I wouldn't look too much into the Java to Kotlin conversion, a human may not write the code that way
आपकी अमरीकी अंग्रेजी हमे बिलकुल समझ नहीं आती हो सके तो विडियो हिन्दी में भी बनाओ
there's an option to turn on subtitles, you can try that
Consider moving the mic away from your mouth or using a screen to reduce the annoying mouth noises you are making
STFU! You have other million videos to watch if you're annoyed.
so companion object is static in java?
you can think of it that way, yes