Chapter 2 · Objects
Packages and projects
Step 1 of 16 · Demo
Two files, again
In this lesson you sort a program's classes into folders, and build them all with one command.
Lesson 6's program: Main calls Greeter.greet. Press Run: javac compiles both files.
Step 2 of 16 · Concept
Packages keep names apart
Without packages, two classes cannot share a name. That gets hard as programs grow. A bank wants an Account
for money, and its login code an Account for user names.
A package is a family name for classes. With it, each class gets a full name:
| class | package | full name |
|---|---|---|
the money Account | app.model | app.model.Account |
the login Account | app.login | app.login.Account |
Scanner (lesson 6) | java.util | java.util.Scanner |
Step 3 of 16 · Concept
A package is a folder
- The classes of the package
app.modellive in the folderapp/model. - Each file starts with its package line:
package app.model;. - A class that uses
Accountfrom another package names it in animportline:import app.model.Account;.
Step 4 of 16 · Quiz
Which package line?
Shelf.java is in the folder app/model. What is its first line?
Step 5 of 16 · Demo
A project with folders
Lesson 7's Account, now in the package app.model, and a Main in app. The editor's file tree shows the
folders: click a file to open it. Press Run.
public appears in new places; the step public crosses packages says why.
Step 6 of 16 · Concept
One javac for the whole folder
Run's first line holds two commands, shortened here:
javac -d out app/Main.java app/model/Account.java
java -cp out app.Main
javacgets every.javafile of the project, with its path.-d outputs the classes inout, in folders that follow the packages.java -cp out app.Mainstarts the JVM. It finds each class inoutby its full name.
Step 7 of 16 · Quiz
Where did it go?
Run compiles the project with this command. Where does javac write the compiled Account class?
javac -d out app/Main.java app/model/Account.java
Step 8 of 16 · Concept
public crosses packages
Main and Account are in different packages now. A word before a class, a field or a method says who can use
it:
| written before it | who can use it |
|---|---|
public | every class, in every package |
| nothing | the classes of the same package |
protected | the same package, and subclasses |
private | its own class only |
So Account, its constructor and deposit are public. balance stays protected, for subclasses.
Step 9 of 16 · Try it
Try it: public is missing
deposit has lost its public. Run it: javac says deposit(int) is not public in Account. Put public back in
app/model/Account.java and run it again.
Step 10 of 16 · Exercise
Your turn: Greeter in a package
Main imports a class Greeter from the package app.text. Make it:
- Right-click the folder
appin the tree, choose New Folder, typetextand press Enter. (Or selectappand click the tree's New Folder button, the folder with a +.) - Right-click
text, choose New File, typeGreeter.javaand press Enter. The file opens with itspackageline and the class filled in. - Write lesson 6's method
greetin it.
The program must print Hello, Ada!.
Main is in another package, so it can use only what is public: the class, and the method too.
The method is public static String greet(String name). It returns "Hello, " + name + "!".
public static String greet(String name) {
return "Hello, " + name + "!";
}
Step 11 of 16 · Exercise
Your turn: savings in the package
Main uses a class Savings that does not exist yet. Right-click the folder model, choose New File and
type Savings.java. Then, as in lesson 7:
- make
SavingsextendAccount, - give it a constructor
Savings(String owner, int balance)that callssuper(owner, balance), - add a method
addInterest()that deposits 5 % of the balance (balance / 20).
The program must print Grace: 105.
Until Savings.java exists, javac says cannot find symbol in app/Main.java. Make the file first.
Main is in app, Savings in app.model: the constructor and addInterest need public.
public class Savings extends Account {
public Savings(String owner, int balance) {
super(owner, balance);
}
Step 12 of 16 · Quiz
Package and folder must agree
In the savings exercise, Main imports app.model.Savings. You make app/model/Savings.java with the first line
package app;. What happens when you press Run?
Step 13 of 16 · Concept
The terminal sees the same folder
Each project block is a real folder in the Linux machine. Look at the one of A project with folders:
- Choose Open in the terminal in its ⋯ menu.
- Type
ls app/model:lslists your source files there. - After a Run, type
ls out/app/model:Account.class, where javac put it. - Paste the line Run printed, without its
$: it builds and runs the same way.
Files you make there show up in the tree when you click back into the editor.
Step 14 of 16 · Check yourself
Check yourself: a shelf of books
Main uses two classes of the package app.model that do not exist yet. Write them so that the program prints
exactly:
Kitchen: 2 books, 886 pages
Two files in app/model. Shelf keeps a count and a page total; Book needs a public method that gives its pages.
Step 15 of 16 · Challenge · optional
Challenge: two Accounts
Optional. It earns the lesson's third star.
The project has two classes named Account: one in app.model, one in app.login. Make main also create a
login Account for "ada" and print it. The program must print:
Ada: 10
login ada
Try import app.login.Account; and run it. javac refuses: one file cannot import two classes named Account.
The full name works without an import, anywhere: app.login.Account is a type like any other.
app.login.Account login = new app.login.Account("ada");
Step 16 of 16 · Recap
Recap
- A package is a family name for classes and a folder:
app.modellives inapp/model. - A file starts with its
packageline;importnames a class from another package. - Another package sees only what is
public; a subclass also sees what isprotected. - One
javac -d outbuilds the whole folder;java -cp out app.Mainruns it by its full name.
Next: lesson 9 is about text: String and what you can do with it.
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
public class Main {
public static void main(String[] args) {
System.out.println(Greeter.greet("Ada"));
}
}
package app;
import app.model.Account;
public class Main {
public static void main(String[] args) {
Account a = new Account("Ada", 10);
a.deposit(5);
System.out.println(a);
}
}
package app;
import app.model.Account;
public class Main {
public static void main(String[] args) {
Account a = new Account("Ada", 10);
a.deposit(5);
System.out.println(a);
}
}
package app;
import app.text.Greeter;
public class Main {
public static void main(String[] args) {
System.out.println(Greeter.greet("Ada"));
}
}
package app;
import app.model.Savings;
public class Main {
public static void main(String[] args) {
Savings s = new Savings("Grace", 100);
s.addInterest();
System.out.println(s);
}
}
package app;
import app.model.Book;
import app.model.Shelf;
public class Main {
public static void main(String[] args) {
Shelf shelf = new Shelf("Kitchen");
shelf.add(new Book("Dune", 412));
shelf.add(new Book("Emma", 474));
System.out.println(shelf);
}
}
package app;
import app.model.Account;
public class Main {
public static void main(String[] args) {
Account money = new Account("Ada", 10);
System.out.println(money);
// also make and print a login Account for "ada"
}
}