Java Fields
Put an int field named fill inside a class Cup in Cup.java. In Shelf.java, build one cup with Cup c = new Cup();, store a number with c.fill = 3;, then print that field with System.out.println(c.fill);. Compile both files and run java Shelf. The screen should show one line: 3. The field holds data on that one object. You reach it through the variable that names the object. Storing and reading that value on the object is what this page teaches.
A place for data on the object
On the objects page, beep only printed. It did not keep a number for later. A field is a name declared inside the class that each object can hold its own value in. After new builds the object, you assign the field through the object variable, then you can print it.
Create Cup.java first. The public class line is public class Cup. Do not leave an older Chip or Badge class name here. Inside the class braces, write one field line: int fill;. There is no method on this first box. There is no main in Cup.java on this page. Save before you compile.
public class Cup {
int fill;
}
Shelf assigns fill, then prints it
Create Shelf.java in the same folder. Its public class line is public class Shelf. Inside main, write three lines in order: Cup c = new Cup();, then c.fill = 3;, then System.out.println(c.fill);. The first line builds the object. The second stores 3 in that object’s fill. The third prints the stored number.
public class Shelf {
public static void main(String[] args) {
Cup c = new Cup();
c.fill = 3;
System.out.println(c.fill);
}
}
From the folder that holds both files, run javac Cup.java Shelf.java. You can compile Cup.java first and Shelf.java second when both are complete. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Shelf — not java Shelf.java, and not java Cup. Leave the .java ending off the run command. Start the class that holds main. The same commands work on Windows and on macOS.
The program should print one line and stop: 3. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in a file. Delete that call and keep the three lines above in Shelf, with the fill field in Cup.
Walk the run once. Java starts at Shelf.main. new Cup() builds one object and stores it in c. The assignment c.fill = 3 writes 3 into that object’s field. println(c.fill) reads the same field and prints 3. The number on the screen is the value stored on the object, not a hard-coded print of a string like "ok".
The field name fill is declared once in the class. Each object built from Cup gets its own slot for that name. When you write c.fill, you are naming the object first, then the slot on that object. That order — object, then field — is the shape this page keeps.
Class, object, and field side by side
Read the three names once in a small table. The field line always goes through an object on this page.
📊 class object field
| Name | What it is on this page |
|---|---|
Cup |
the class / type in Cup.java |
c |
one object built with new Cup() |
c.fill |
the int stored on that object |
Writing Cup.fill = 3; without an object does not match this beginner shape. The field belongs to each object after new. Build first, then assign through the variable. Writing only new Cup(); without storing the result also leaves you no name for fill. Keep Cup c = new Cup(); then c.fill = 3;.
A leftover objects demo can confuse the folder. A file that still calls c.beep(); with a Chip class is the previous lesson. For this page keep Cup and Shelf and the fill field. You do not need beep here.
If you print c itself with System.out.println(c);, the screen will not show a clean 3. It prints a default object text. To see the stored number, print c.fill.
Two cups keep separate fills
You can build two objects from Cup. Each has its own fill. Changing one does not change the other.
In Shelf, you can write Cup a = new Cup(); and Cup b = new Cup();, then a.fill = 2; and b.fill = 9;, then print a.fill and b.fill. After javac Cup.java Shelf.java and java Shelf, the screen should show 2 on one line and 9 on the next. Both objects share the class text that declares fill, but each object holds its own number.
If both prints show the same value when you meant two different numbers, check that you assigned both fields and that you did not write Cup b = a;. That line makes b refer to the same object as a, so both names share one fill. On this page use two new Cup() expressions when you want two separate fills.
You do not need a third boxed program for that practice. Edit Shelf carefully, recompile, and run again. Then you can restore the single-cup box if you want the quiz’s first shape.
What constructors will add later
This page assigns the field after new with a separate line: c.fill = 3;. A later page — constructors — will show another way to set starting values when the object is built. Stay with the plain field and the assignment after new here. Do not invent a constructor method on this page.
Default values exist for fields you never assign: an int field starts at 0. If you print c.fill before any assignment, the screen can show 0. This page still teaches the assignment you write yourself, so keep c.fill = 3; before the print in the main demo.
If javac cannot find the symbol Cup while compiling Shelf.java, check that Cup.java sits beside Shelf.java and that the class line is public class Cup. If the screen shows 0 when you expected 3, the assignment line may be missing or may run after the print — put c.fill = 3; above println(c.fill).
Before constructors
You can declare int fill; inside Cup, build Cup c = new Cup();, assign c.fill = 3;, and print c.fill from Shelf. Compile both files and run java Shelf. The screen shows 3. Two objects can hold two different fills. The next page is constructors, where you begin setting values at the moment new builds the object.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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