Loading...

Java Records

Declare a compact data type with record and read its components. In Card.java, keep a package-private record Tag(String name) {}, keep the public class Card, build Tag t = new Tag("oak");, and print t.name(). Compile with javac Card.java and run java Card. The screen should show oak. A second demo adds an int component so the screen shows oak, then 3. Leave optional JUnit for the next page. This lesson is a record with accessors, not a Streams pipeline and not a Lambda forEach walk.

One component, one accessor

Open Card.java. Keep the public class name Card. Delete leftover Pipe streams demos, leftover Cue lambda prints, leftover Bin generics lists, and leftover Box Scanner prompts if they bury the records story. The program must not wait for typing and must not start a stream pipeline.
Above the public class, declare the record:
record Tag(String name) {}
Inside Card.main, build one instance and print the accessor:
Tag t = new Tag("oak");
System.out.println(t.name());
The record header lists components in parentheses. Java generates the constructor and the accessor name() for that component. The screen shows oak.

Card record Tag prints oak

java
record Tag(String name) {}

public class Card {
    public static void main(String[] args) {
        Tag t = new Tag("oak");
        System.out.println(t.name());
    }
}

From the folder that holds Card.java, run javac Card.java. When braces and parentheses match, javac writes no line of its own. Then run java Card — not java Card.java and not java Tag. Keep one public class so the run name matches Card. The record Tag stays package-private in the same file.
Walk the run once. Java starts at Card.main. new Tag("oak") stores the component. t.name() returns "oak". println writes oak. Nothing calls stream(). Nothing calls forEach. Nothing reads args[0].
Say the beginner rule while you watch the screen: record names the type and its components; the accessor matches the component name; construct with new. Do not invent JUnit demos on this page. JUnit waits for the next optional lesson. Do not replace the record with a long hand-written class full of getters in the teaching file — keep the record header.
If you still see OAK then PINE from a leftover Pipe streams file, switch to Card and keep t.name(). If javac complains about two public types, keep only Card public and leave record Tag without public.

Two components, two prints

Keep the same public class Card. Expand the record to two components:
record Tag(String name, int n) {}
Tag t = new Tag("oak", 3);
System.out.println(t.name());
System.out.println(t.n());
Compile again with javac Card.java and run java Card. The screen shows oak, then 3. Each component gets an accessor with the same name. Order in the header matches the constructor arguments.

Card record Tag prints oak then 3

java
record Tag(String name, int n) {}

public class Card {
    public static void main(String[] args) {
        Tag t = new Tag("oak", 3);
        System.out.println(t.name());
        System.out.println(t.n());
    }
}

Walk the run once. name() returns "oak". n() returns 3. Keep the two demos as two teaching shapes. First prove one component. Then prove two components. Both shapes share a package-private record beside public Card.
If you swap constructor arguments, the values land in the wrong components. Keep "oak" first and 3 second in the teaching file you keep.

Record habits

Keep the compact header and the accessors together while you practice.

📊 Record habits for Card

Habit Shape Beginner check
Declare record Tag(String name) {} components sit in the header
Construct new Tag("oak") arguments match component order
Read t.name() accessor name matches the component
Two fields record Tag(String name, int n) {} each component has its accessor
Public class public class Card run with java Card, not java Tag

Records is not Streams and not JUnit

Streams filtered and mapped list values through a pipeline. Records hold a fixed set of components in a compact type. JUnit will check behavior with tests on a later optional page. Do not mix those three jobs in one beginner file today.
oak here is a record component value returned by name(). It is not a stream filter result and not a Lambda forEach visit alone. When a leftover Pipe file still prints uppercase mapped words, leave that as history and keep Card's t.name() line.
Without the record keyword, this page's compact header is gone. Without matching constructor arguments, javac rejects the call. Keep the header and the new line aligned while you practice each demo. Keep JUnit annotations off this page until that lesson, even if you have already seen a testing headline elsewhere in the optional path. Prefer the short record shape so the screen stays tied to name() and n() on every careful read through the teaching file.

Folder checks before JUnit

List the folder after a successful compile. You should see Card.java and Card.class together, and usually a Tag.class for the record. Edit the source, save, run javac Card.java, then run java Card. A stale .class can show an old one-line screen if you skip compile after adding n.
If javac complains about duplicate public classes, keep only Card public. If the screen shows Tag[oak] instead of oak, you printed the whole record — keep the teaching print as t.name(). If nothing prints, confirm main still calls println.
Do not paste Pipe stream().map into Card. Do not paste Cue forEach as a replacement for the record story. The optional records job is a compact type with accessors.

Before optional JUnit

You can keep record Tag(String name) {}, build new Tag("oak"), print t.name() as oak, then use record Tag(String name, int n) {}, print oak and 3. Compile with javac Card.java and run java Card. The table separates declare, construct, and read. The next optional page is JUnit.
Keep the Card demos for a clear records pair. Keep Pipe streams files off this page when you practice records. Keep Cue lambda files off this page when you practice records. Keep JUnit work off this page until that lesson. Those habits are the checkpoint before you move on. When the one-field screen shows oak or the two-field screen shows oak then 3, Records did its job for this lesson and you are ready for JUnit if you continue the optional path.
One last pass through the folder: one public Card, package-private record Tag, matching constructor arguments, accessor prints, no Scanner wait, no stream pipeline required. Run javac Card.java and java Card once more on the demo you keep open. If the screen still shows oak or oak then 3, the Records shape is solid and the course can move to optional JUnit on the next page. Prefer one clear record habit per read so each pass teaches the same map.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top