Appendix C. leftovers: The Top Ten Things: (We Didn’t Cover)
Even after all that, there’s still a little more.
There are just a few more things we think you need to know. We wouldn’t feel right about ignoring them, and we really wanted to give you a book you’d be able to lift without training at the local gym. Before you put down the book, read through these tidbits.
1. Packages and imports
As we said in Chapter 9, classes and functions in the Kotlin Standard Library are grouped into packages. What we didn’t say is that you can group your own code into packages.
Putting your code into packages is useful for two main reasons:
* It lets you organize your code.
You can use packages to group your code into specific kinds of functionality, like data structures or database stuff.
* It prevents name conflicts.
If you write a class named
Duck, putting it into a package lets you differentiate it from any otherDuckclass that may have been added to your project.
How to add a package
You add a package to your Kotlin project by highlighting the src folder, and choosing File→New→Package. When prompted, enter the package name (for example, com.hfkotlin.mypackage), then click on OK.
Package declarations
When you add a Kotlin file to a package (by highlighting the package name and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access