Kotlin companion object - common use cases

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video i want to talk about the companion object in kotlin if you've been doing kotlin or android development you've probably seen this in your code so in this video i want to talk about two of the most common use cases for the companion object so first off the most common use is if you need a property needed at a class level and not a specific instance so typically with object oriented programming every object of a class which means every instance of a class will have some state associated with it but if you only need one copy of that information which is shared across every instance of that class then that's a perfect use case for a companion object and you've probably seen this already in android programming you'll typically have a companion object and inside you'll have a tag which is usually the name of the class so in this example your bank account so the tag is called bank account and now you can use this tag when you're printing out a log statement so in this example here i just have a print line just to prove to you that i don't need to create an instance of bank account i can directly access tag and then we can see an output bank account is printed out as we'd expect and the other common way of using this is in any method of bank account for example in the tostring method i can now reference tag just like any normal variable and so going back to the main method if i create a bank account with rahul my name as the constructor parameter here and i print that out now because we've over in the two string method i'd expect to see bank account with name brahl four times so if we try this out so we do indeed see bank account with my name four times we can use companion objects for a lot more than just constants though for example we may want every new bank account created to have a globally unique id let's create a private var next id which is in the companion object and we'll use next id to compute the value of a member variable called account id so in the init block which is the code that gets done every time we create a new bank account we're going to run this code which basically says let's assign the value of account id to next id and also after we do that let's increment next id so every time we create a bank account we're going to have a unique account id and then next id is shared globally across all bank account objects let's test this out by adding this in to the tostring method and if we run the program now we can see that each bank account has a unique id by using the shared state of the companion object so if you're coming from a java background you can kind of think of companion object as similar to the static keyword in java there is no static in kotlin so we achieve the same effect using companion objects another use case for the companion object is something called the factory pattern which is a way to perform extra work before an object can be used oftentimes after constructing an object we may want to do some sort of validation or post processing before we allow that object to be used so in those circumstances what we'll do is make the constructor of that class private so in our example you could imagine how we want the bank account before we give it to someone we want to do some sort of identity validation that this is a legitimate bank account to be opened and as soon as we do that you'll notice that we get an error when trying to construct the bank account like normal to get around that we're going to add a function inside of the companion object this will take in a name string as a parameter which is what we'll use in order to actually call the constructor internally and what we're doing here is after constructing the banner account we're going to validate the identity of that person and if they have a valid identity we'll return the instance of the bank account otherwise we'll return null so that's why we have a nullable bank account as a return type here so the validate identity method you could imagine this actually checking their government id or going to some database to validate their identity but just to keep it simple i'm returning true always here so it's a boolean return type and we'll get back the bank account so now when a client when someone actually wants to use our bank account class the way they would have to do that is call the dot create method and it takes in this one parameter same as before so there's actually no functional change here i'll run this and you can see the identical output show up but the power of the factory pattern is that now you can do any kind of validation or post processing you need to rather than relying on the client to call methods in a certain order you'll sometimes see this in libraries that you might be using or you can also employ it in your own application that's all i had for this video as a review the most common use cases for a companion object are properties or functions that you need at the class level and not for a specific instance and second the factory pattern where you can perform validation before returning the object back to the consumer if you have any questions or comments i'd love to hear from you if not thanks for watching and i'll see you later bye
Info
Channel: Rahul Pandey
Views: 2,207
Rating: 5 out of 5
Keywords: android, development, programming, kotlin, code, tutorial, rahulpandey, rahul, pandey, teach, intellij, companion object, companion kotlin, kotlin object, kotlin static variables, kotlin static, kotlin class variable, kotlin member variable, kotlin factory pattern, factory pattern
Id: Dt8zTBdDv5w
Channel Id: undefined
Length: 5min 10sec (310 seconds)
Published: Sun Mar 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.