Java Instance Methods
Write a class Jar in Jar.java with an int field fill, a constructor Jar(int n) that sets fill = n;, and an instance method void bump() that runs fill = fill + 1;. In Desk.java, build one jar with Jar j = new Jar(3);, call j.bump();, then print j.fill. Compile both files and run java Desk. The screen should show one line: 4. The method runs on that one object and changes its field. Calling a method through the object variable is what this page teaches.
A method that belongs to the object
On the methods pages earlier you called a name from main without an object in front. Here the method sits on the class, but you reach it through a jar variable: j.bump(). That call uses the fields of the jar that j names. A second jar would keep its own fill.
Create Jar.java first. The public class line is public class Jar. Do not leave an older Tin or Box class name here. Inside the braces declare int fill;. Write the constructor Jar(int n) { fill = n; } with different names so this page does not need this. Then write the instance method:
void bump() { fill = fill + 1; }
There is no main in Jar.java. Save before you compile. The method header needs void because bump does not return a value to print. The constructor still has no void — keep that difference clear.
public class Jar {
int fill;
Jar(int n) {
fill = n;
}
void bump() {
fill = fill + 1;
}
}
Desk builds a jar and calls bump
Create Desk.java in the same folder. Its public class line is public class Desk. Inside main, write three lines in order: Jar j = new Jar(3);, then j.bump();, then System.out.println(j.fill);. The constructor stores 3. The call j.bump() adds one. The print shows the new value.
Keep Desk as the class that holds main. Do not put main inside Jar for this lesson. If a leftover Lab file from the this page still sits in the folder, do not run java Lab for this lesson — run java Desk after you compile Jar.java and Desk.java.
public class Desk {
public static void main(String[] args) {
Jar j = new Jar(3);
j.bump();
System.out.println(j.fill);
}
}
From the folder that holds both files, run javac Jar.java Desk.java. You can compile Jar.java first and Desk.java second when both are complete. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Desk — not java Desk.java, and not java Jar. 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: 4. 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 new Jar(3), j.bump(), and the print in Desk, with bump in Jar.
Walk the run once. Java starts at Desk.main. The expression new Jar(3) builds a jar and stores 3 in fill. The call j.bump() enters bump on that same object. The body writes fill = fill + 1, so fill becomes 4. Then main prints j.fill and the screen shows 4.
If you write bump(); with no j. in front, javac refuses in this shape of main. The instance method needs an object. If you write Jar.bump(); as if it were a static call, that is the next lesson’s idea — not this page. Keep j.bump();.
Compared with this and with plain field assign
On the this page you stored a field inside the constructor with matching names. That still works. This page adds a method you can call later, after the object already exists. A leftover Tin t = new Tin(7); with this.size = size; is a different lesson. For this page keep Jar and Desk and j.bump();.
You could still write j.fill = j.fill + 1; in main. This lesson moves that change into bump so the object carries the action. Printing j alone with System.out.println(j); does not show a clean 4. Print j.fill to see the stored number.
Two bumps on one jar
You can call j.bump(); twice before you print. Start with new Jar(3), bump once to 4, bump again to 5, then print. That check fits in a small second desk demo.
public class Desk {
public static void main(String[] args) {
Jar j = new Jar(3);
j.bump();
j.bump();
System.out.println(j.fill);
}
}
Compile and run again: javac Jar.java Desk.java, then java Desk. The screen should show 5. Each call to bump ran on the same object and added one more. Writing Jar k = j; would not build a second object — use two new Jar(...) calls when you want two separate fills. For this page, two bumps on one jar is enough to see the method run twice.
If the screen still shows 3, the bump calls may be missing or main may still print before calling them. If it shows 7 or another leftover number, an older Tin demo may still be in the file. Keep new Jar(3) and the bump calls.
What this leaves for static
This page calls the method through an object: j.bump(). A later page — static — shows methods and fields that belong to the class name itself without an object in front. Stay with the instance call here. Do not invent static void bump() on this page unless you already know it; the lesson goal is the call on the object.
If javac cannot find Jar while compiling Desk.java, check that Jar.java sits in the same folder and that the class line is public class Jar. If the screen shows 0, the constructor may be missing the store, or bump may never run — keep Jar(int n) { fill = n; } and j.bump(); before the print.
When the folder still holds Tin.java or Lab.java from the this page, leave them alone or delete them only if they confuse your compile command. Run java Desk after you compile the Jar and Desk pair from this lesson.
Before static
You can declare int fill;, write Jar(int n) { fill = n; } and void bump() { fill = fill + 1; }, and from Desk run Jar j = new Jar(3);, j.bump();, then print j.fill. Compile both files and run java Desk. The screen shows 4. The instance method ran on that object and changed its field. The next page is static, where some members belong to the class name without needing an object variable in front.
Keep void on the method, not on the constructor. Keep the object before the call: j.bump();. Keep the print on j.fill. Those three habits are the checkpoint for this page before you move on.
🧠 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