Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Chapter 13: Packages and Imports > 13.1 Putting code in packages - Pg. 237

Chapter 13 Packages and Imports When working on a program, especially a large one, it is important to min- imize coupling--the extent to which the various parts of the program rely on the other parts. Low coupling reduces the risk that a small, seemingly innocuous change in one part of the program will have devastating conse- quences in another part. One way to minimize coupling is to write in a modular style. You divide the program into a number of smaller modules, each of which has an inside and an outside. When working on the inside of a module--its implementation--you need only coordinate with other pro- grammers working on that very same module. Only when you must change the outside of a module--its interface--is it necessary to coordinate with developers working on other modules. This chapter shows several constructs that help you program in a modular style. It shows how to place things in packages, make names visible through imports, and control the visibility of definitions through access modifiers. The constructs are similar in spirit to constructs in Java, but there are some differences--usually ways that are more consistent--so it's worth reading this chapter even if you already know Java. 13.1 Putting code in packages Scala code resides in the Java platform's global hierarchy of packages. The example code you've seen so far in this book has been in the unnamed package. You can place code into named packages in Scala in two ways. First, you can place the contents of an entire file into a package by putting a package clause at the top of the file, as shown in Listing 13.1. 237