Java Objects
Write a class named Chip in Chip.java with a method beep that prints ok. The method is not static on this page. In a second file Bench.java, build one chip with Chip c = new Chip();, then call c.beep();. Compile both files and run java Bench. The screen should show one line: ok. The word new builds one object from the class. The variable c holds that object. You call beep on c, not on the bare class name. That one built object — and the call that uses it — is what this page teaches.
One object from the class
A class is still the named type. An object is one value built from that type. The line new Chip() asks Java to build that value. You store it in a variable whose type is Chip, then you use the variable to reach methods that belong to each object.
Create Chip.java first. The public class line is public class Chip. Do not leave an older Badge or Desk class name in this file. Inside the braces write void beep() — no static word on this header. The body is one print: System.out.println("ok");. Keep void. The method does not return a number. There is no main in Chip.java on this page. Save the file before you compile.
public class Chip {
void beep() {
System.out.println("ok");
}
}
Bench builds the chip, then calls beep
Create Bench.java in the same folder. Its public class line is public class Bench. Inside main, write two lines: first Chip c = new Chip();, then c.beep();. The first line declares a variable c of type Chip and fills it with a new object. The second line calls beep on that object. The dot sits after the variable name, not after the class name alone.
public class Bench {
public static void main(String[] args) {
Chip c = new Chip();
c.beep();
}
}
From the folder that holds both files, run javac Chip.java Bench.java. You can compile Chip.java first and Bench.java second when both files are complete. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Bench — not java Bench.java, and not java Chip. 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: ok. 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 Chip c = new Chip(); plus c.beep(); in Bench, with beep in Chip.
Walk the run once. Java starts at Bench.main. The expression new Chip() builds one object. That object is stored in c. The call c.beep() enters beep for that object. The method prints ok and returns. Bench then ends. The screen line came from the object’s method, after new created the object.
Think of the class as the blueprint on disk and the object as one built piece in memory for this run. You compile the blueprint once. Each new Chip() builds another piece. The variable names the piece so later lines can reach it. Without that name, the built piece has no handle for beep on this page.
Why the call uses c, not Chip alone
On the classes page, a static method could be called as Badge.show() with the type name. Here beep has no static word. Java expects an object first. Writing Chip.beep(); without an object does not match this beginner shape. Build with new, keep the result in a variable, then call through that variable.
If you write only new Chip(); and never store it, you still built an object, but you have no name left to call beep on. On this page keep the variable: Chip c = new Chip(); then c.beep();. The variable is how you reach the object after new finishes.
A leftover static demo can confuse the folder. A file that still calls Badge.show(); is the previous lesson’s shape. For this page keep Chip and Bench and the new line. You do not need Badge here.
Two objects, two beeps
You can build more than one object from the same class. Each new Chip() makes a separate object. Each object can run beep on its own. The class text is shared; the objects are not the same value.
public class Bench {
public static void main(String[] args) {
Chip a = new Chip();
Chip b = new Chip();
a.beep();
b.beep();
}
}
Compile and run again: javac Chip.java Bench.java, then java Bench. The screen should show two lines: ok, then ok. The first line came from a. The second came from b. Both used the same beep method text in Chip, but each call ran on a different object built with new.
If the screen shows only one ok, check whether both calls are present and whether both variables were built with new. Writing Chip b = a; does not build a second object; it makes b refer to the same object as a. That idea waits for later pages. On this page use two new Chip() expressions when you want two objects.
What this page leaves for fields
This page does not store a number or a word inside the chip yet. Fields — names that hold data on each object — are the next page. Here the object exists after new, and the instance method beep only prints. That is enough to see the split between the class as a type and one object as a built value.
You can rename the variable from c to unit if you like: Chip unit = new Chip(); then unit.beep();. The screen stays ok when the method body still prints that word. The variable name is yours; the type name Chip and the new Chip() expression must still agree.
If javac says it cannot find the symbol Chip while compiling Bench.java, check that Chip.java sits in the same folder and that the class line really says public class Chip. If the run prints nothing, confirm main still calls c.beep(); after the new line, and that beep still contains the println.
Before fields
You can write Chip with void beep(), build one object with Chip c = new Chip();, and call c.beep(); from Bench. Compile both files and run java Bench. The screen shows ok. A second new Chip() builds another object that can beep on its own. The next page is fields, where each object begins to hold its own stored data.
🧠 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