Your First Kotlin Android App: An App From Scratch

Aug 15 2023 · Kotlin 1.8.20, Android 13, Android Studio Flamingo | 2022.2.1

Part 2: Manage Data in Jetpack Compose

12. Learn About Instances, Data & Functions

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Introduction Next episode: 13. Handle a Click Interaction

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 12. Learn About Instances, Data & Functions

At this point, you’ve learned how to use composable functions to build the basic user interface for your app. It’s time to take a deeper look at the code that you use to create Composables and Modifiers.

But before you do that, it’s important to understand some programming theory about how this all works.

I know some of the code you added in the last part might not make sense to you yet. You need some understanding of the Kotlin programming language to understand more about what you did.

In this episode, you’ll learn about 3 key concepts of the Kotlin programming language: instances, data, and methods.

If you’re already familiar with Object Oriented Programming and feel comfortable working with one - at least from a high-level point of view - you might feel like skipping this but I’ll personally encourage you to watch this episode.

Who knows, maybe you might pick up a thing or two or it could help as a refresher for the knowledge you have.

When developing apps, programmers like to group related data and functionality into small pieces. For example, lets say you want to display a list of dogs in your app. To do this, you first need to define a template for what you need to know about each dog, for example, its name and its breed.

Once you have that template, you can create multiple instances of that template.

You can think of instances as just filled-in versions of that template. For example, you might create an instance for Obi the Poodle and another instance for Datti the Maltese.

This template or blueprint is called a class. To define a class you use the keyword class.

Before Jetpack Compose, user interfaces were created using Views. These widgets were instances of a different classes. For example you have the TextView, Button and ImageView and these are all defined as classes.

Do note that instances of a class are also know as objects. So a dog instance can be called a dog object. And a TextView instance can also be called a TextView object.

But in Jetpack compose, these widgets are functions. You’ve not created a class by yourself yet but permit me to explain the concept of classes using the widgets you’re already working with even though we know that they’re are functions. And not to worry, you’ll understand functions better as you follow along this episode.

A class or an instance of a class can have data and functionality. For example, take the hit me button in your app.

First, it has some data like the text to display, its position on the screen, its width and height and so on. The data elements in a class are known as its properties.

Second, it has some functionality, such as the ability to recognize when a user taps on it, display an animated ripple effect when tapped, and also trigger an action in response to the tap.

These functionalities are commonly known as functions, no pun intended.

And when they’re declared inside a Class, they’re called member functions or methods.

You’ve used and created functions already. All the composables you used to build the UI are all functions. The GameScreen composable you created is also a function.

If you have an instance of a class, you call a method on it by appending a dot and the methodName, and enclosing any arguments you need to send to the method in parentheses.

This is similar to how you called Modifier methods like fillMaxSize() and padding().

Now, the concept of methods may still feel a little bit weird to you, so let’t take a look at an example.

You (or at least an instance named “You”) wants to throw a party. You clean the room, and put on some music. But you forgot to buy some cookies!

Fortunately, you’ve invited the instance named Steve who happens to live next door to a convenience store. So you call a method on Steve asking him to bring you some cookies.

The processor now switches it’s attention to the object Steve and executes the commands from his buyCookies() method, one by one, from top to bottom.

When the buyCookies() method is done, the computer returns to your throwParty() method and continues where you left off, so you and your friends can eat the cookies that Steve brought back with him.

The Steve instance also has data. Before he goes to the store he has money.

At the store he exchanges this money data for other, much more important, data: “cookies!” After making that transaction, he brings the cookies back over to the party, and you add them to your drink and snack table.

If he eats the cookies along the way, well - your program has a problem popularly known as a bug.

Another important concept when working with methods is that you’ll often need to change their default behavior by doing what is called an override.

This is generally the case when your class inherits from a parent class. That is, deriving the data and methods in your class from the parent class.

For example, the Steve instance can inherit a few methods from its parent class.

You’ve already seen an example of overriding. The onCreate method inside the MainActivity is an overriden method from the parent class: ComponentActivity.

It must be overriden because all activties in Android are required to set a window to display views whenever an activity is created.

Wow!!! That was a lot of information in just one episode. Let’s review some key points.

The most important thing to remember from this lecture is that instances of classes contain two things. First,

  • data, like the money to buy some cookies,
  • and second, methods - like the steps involved in buying some cookies, and remember methods are just functions that are contained inside a class.

Instances can look at each other’s data - well, to some extent at least. After all, Steve may not approve of it if you peek inside his wallet.

Asking instances to perform their functions is how you get your app to do things.

Remember, you build user interfaces in Jetpack Compose with functions but its good to know about classes and instances as you’ll use them down the learning path.

Don’t worry if you don’t get everything right away or if you have trouble understanding some of the syntax.

There’s a lot of concepts here that may be brand new to you.

We’ll review these concepts again and again throughout this course and the rest of the courses in our learning path until they feel like second nature to you.

Again: it’s all about learning via repetition.