Check out a free preview of the full Introduction to Kotlin and Android Development course
The "Other Data Types" Lesson is part of the full, Introduction to Kotlin and Android Development course featured in this preview video. Here's what you'd learn in this lesson:
Maximiliano discusses other data types including the companion objects attached to classes, sealed classes, enum classes,and data classes.
Transcript from the "Other Data Types" Lesson
[00:00:00]
>> So classes don't have a static member. You know what a static member is of a class in Java or other languages? Where you apply functions and constants or variables, to the class itself like global members, instead of two instances. But here in Kotlin, what we do have is an object within the class known as the companion object, okay?
[00:00:25]
So we have a class and each class has a companion object that goes with the class, okay? So we are together in this life, and the companion object is an object with variables, constants and functions, that then you can apply to the class and not to the options.
[00:00:44]
For example, we have the printAll in the user class. So the companion object is the static of Java. Everything you put in the companion object, it's available through the class name, and not through instances of that class. Does it make sense?
>> So it's not an inner class?
[00:01:04]
>> It's not an inner class, no. It's an object so it's an instance, when you see the object keyword it's an instance, not the class. So what we put inside is actually an object that is applied to the class.
>> So its like an anonymous object?
>> It's like an anonymous object, yeah, but you can have only one over the class.
[00:01:29]
And actually, there are ways to convert Kotlin to Java and Java to Kotlin, okay? In fact, fun fact, if you look for some Java code, Java code for, I don't know, whatever, algorithm. Let me see if I can see some code quickly. These are Java, right? So if I copy Java code, and I am not sure if it's gonna work in this file, let me try.
[00:01:57]
Yeah, it works. So when you paste Java code into a Kotlin file, And the studio is automatically converting that Java code into Kotlin. Even we could see how that changes, because it's interchangeable if you want. So, when you are moving from Kotlin to Java, the companion object becomes static members of the class.
[00:02:25]
So collection, it's actually a property, a static property of user. I'm simplifying the explanation, but it's kind of what's going on.
>> So somewhere in there, you can do user.collection operate on it?
>> Yep.
>> Within the class?
>> Within the class or outside of the class, because there is the companion object is public.
[00:02:49]
So unless you make something private, implicitly you can. But outside, I can do this, or here I can also access the collection. I don't need the prefix user, but having in mind that collection I will actually be available for all the users. So all the users are sharing the companion object is one object for all the users, okay.
[00:03:13]
So, all the instances of that class will have access to the same companion object.
>> And that will be initialized when the class is initialized?
>> Yeah, the companion object it's automatically created when the class is initialized. Actually, when you start the app, because you don't need to use the class.
[00:03:32]
You don't need to create instances of the class, Okay? So, it's like a global. The same happens with static methods. In JavaScript, so if you're coming from JavaScript, this is like, I don't know if you create a class, this is JavaScript, right? So if you have a class in JavaScript, no parenthesis, sorry, then you can apply methods directly to the class, like this.
[00:03:58]
So these are similar to static, and now JavaScript supports also the static keyword. It's kind of new in ECMAScript, okay? So you can also implement similar solutions in JavaScript, in case you're coming from JavaScript, okay? We have seal classes. A seal class is a class that can have no instances.
[00:04:19]
So actually, and it's not abstract. Typically a seal class is only with a companion object. Typically, it's been used for storing objects, global utilities. Okay, so it's like a place, because its useful because in Android we cannot have global functions drop anywhere. So we need a container. Well, a sealed class it's kind of a container, and because it's sealed we cannot create instances of it.
[00:04:55]
So, I mean a compiler will say no, this is a sealed class. Don't create an instance of utilities. Just use the members of it, okay? We have enum classes. Is weird that is called enum class and not enum alone, okay? But it's an enum as you're used to in other languages.
[00:05:14]
The difference is that it's a class because actually it's a class behind scenes. So we can create a direction and then it accepts a set of values, and also each value can have an inner value and a raw value assign. For example, you can say class color of integer.
[00:05:35]
And finally, data class. So what a data class is, it includes basic behavior for classes using data, holding data, they're going to use that data. So for example, it has a better toString. By default, toString of a class will give you the name of the class. Completely useless.
[00:05:58]
Well, a data class will re-implement the toString method and it will show you all the variables with its values. It also includes equals. So then you can use equals equals, and it is going to match its property. So if you have two products, it will give you true if they have the same values even if there are two instances.
[00:06:21]
Okay, make sense? And also copy, copy to clone. So you can call copy automatically. So when you have classes that are holding data, it's better to use data class in Kotlin, because we have better built-in utilities and solutions available for you. Okay, makes sense? Kind of? Actually, we have just over 70% of Kotlin.
[00:06:52]
So you can see it's pretty straightforward. There are more things, more advanced topics, but actually with this we can start playing on creating our user interface.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops