A program can be organized into several source files without becoming several independent programs. Main.java supplies the entry point, while Session.java defines a model Main uses. This is a useful place for a boundary: main chooses objects and actions; the model stores state and implements its own behavior.
In this editor, click a filename tab to read or change it. The marked entry is Main.java. Run starts that file and resolves the helper class from Session.java. Changing tabs does not change which file runs. Both tabs form one program, and each Run starts with fresh object and static state.
Match names and keep files flat
Name each helper file after its class, including capitalization. A public class Session belongs in Session.java. If the names don't match, the compiler reports an error in that file.
Imports belong to the file that uses the imported name. If Session needs an ArrayList later, put the import in Session.java; importing it in Main does not make it visible everywhere.
Follow the boundary
Read Main first, then open Session and follow the constructor and methods it calls. Predict both summary lines before running. These fixed values are trusted; validation of arbitrary input comes next day.
Check your prediction
Java: 25 min, done
Reading: 10 min, plannedMain constructs two independent objects, then completes only the Java session. Session owns the summary wording and completion field. Open the Session.java tab, change min to minutes in summary, and rerun to see both lines change.
Put your objects in a list
Day 4 used a list of Strings. You can use your own Session type in the same
place: ArrayList<Session> holds Session objects. The list manages positions;
each Session still manages its own completion state. This example builds on the single-file Session from Private State & Methods,
so you can trace the list behavior without switching tabs.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Session first = new Session(25);
ArrayList<Session> sessions = new ArrayList<>();
sessions.add(first);
sessions.add(new Session(10));
System.out.println(sessions.size());
System.out.println(first.remaining());
// get returns the object at index 0, not a separate copy.
Session selected = sessions.get(0);
selected.complete();
for (Session session : sessions) {
System.out.println(session.remaining());
}
}
}
class Session {
private final int minutes;
private boolean done;
Session(int minutes) {
this.minutes = minutes;
}
public void complete() {
done = true;
}
public int remaining() {
if (done) {
return 0;
}
return minutes;
}
}Check your prediction
2
25
0
10first and selected refer to the same Session, so completing selected also changes what first observes. The other Session stays unchanged. Trace the for-each loop: on each iteration, session is the next Session object, so you can call its methods.
You may also see sessions.get(0).complete(). Read it in two steps: get the object
at index 0, then call complete on that object. It does the same work as the
selected variable and complete call above. Try replacing those two lines with
the shorter call, then run again. Index 0 must exist before either form is safe.
public class Main {
public static void main(String[] args) {
System.out.println(label(25));
System.out.println(label(10));
}
static String label(int minutes) {
return minutes + " min";
}
}Check your prediction
25 min
10 minA small calculation can be checked separately from file organization. Separating source files should clarify responsibilities; it should not make every minor expression its own class.
Trace construction across a file boundary
Follow the value 25 from the two-tab example above:
| Step | File | Where 25 is |
|---|---|---|
new Session("Java", 25) | Main.java | an argument |
| constructor runs | Session.java | the minutes parameter, then the private field |
summary() runs | Session.java | read from the field into the text |
println | Main.java | inside the returned String |
The trace is the same as when both classes were in one file: splitting files organizes definitions, it does not change what runs. Each editor on this page also has its own copy of its tabs, so an edit in one editor does not reach another.
Your turn
Rebuild the collaboration
This workspace has two tabs, Main.java and Topic.java. In Topic.java, give
Topic a private final String name supplied by its constructor, and a public
String label() that returns Topic: followed by the name. In Main.java,
construct a Topic for Java and print its label:
Topic: JavaThen check which file a mistake is reported in: misspell label in Main.java's
call, run, and read which filename the error names. Put it back, then misspell
name inside label() in Topic.java and run again.
Hint 1: the idea
The class collaboration is the same whether the helper is below Main or in a correctly named separate file.
Hint 2: a first step
Start in Topic.java: copy the shape of Session.java from the example above (a
field, a constructor that stores its parameter, one method). Then switch to the
Main.java tab and write the new Topic("Java") line.
Show one solution
Main.java:
public class Main {
public static void main(String[] args) {
Topic topic = new Topic("Java");
System.out.println(topic.label());
}
}Topic.java:
public class Topic {
private final String name;
public Topic(String name) {
this.name = name;
}
public String label() {
return "Topic: " + name;
}
}Topic: JavaMain.java only knows Topic's public constructor and label(); the name field stays inside Topic.java. The misspelled call is reported in Main.java and the misspelled field in Topic.java, because each error belongs to the file that contains the broken line.
Change it and explain
In the two-tab example, deliberately misspell summary in Main: the compiler points to Main.java. Restore it, then misspell minutes inside Session.summary: the diagnostic should name Session.java. Restore each defect before continuing. Never treat a previous successful run as evidence that new edits also work.
There are two useful debugging questions: which file owns the failing behavior, and which call supplied the wrong value? A stack trace or diagnostic helps you follow that connection. The final project will add a collection owner and a separate test entry, keeping the same small-file pattern.
Going further: how the playground finds files
The playground keeps file names flat: no packages or folders. A mistyped entry or a nested path is reported as a layout error rather than running a different file. As with Java's own source launcher, a helper file that nothing uses may never be compiled, so the presence of a tab does not prove it was checked.
Completion is saved in this browser only. It does not measure mastery.