Java this
Write a class Tin in Tin.java with an int field size and a constructor Tin(int size) that sets this.size = size;. In Lab.java, build one tin with Tin t = new Tin(7);, then print t.size. Compile both files and run java Lab. The screen should show one line: 7. The parameter and the field share the name size. The word this names the field on the object so the store reaches the right place. Using this when names collide is what this page teaches.
Why the same name needs this
On the constructors page the parameter was n and the field was size, so size = n; was clear. Here both names are size. Inside the constructor, a bare size means the parameter. Writing size = size; copies the parameter onto itself and leaves the field at its default. The field on the object still needs a write. That write uses this.size on the left side.
Create Tin.java first. The public class line is public class Tin. Do not leave an older Box or Cup class name here. Inside the braces declare int size;. Then write the constructor: Tin(int size) { this.size = size; }. There is no main in Tin.java. Save before you compile.
Read the assignment once more. The right side size is the value that arrived in the parameter — here 7. The left side this.size is the field on the object being built. After the line runs, that object’s size holds 7. If you drop this. and keep size = size;, the screen later shows 0 because the field never changed.
public class Tin {
int size;
Tin(int size) {
this.size = size;
}
}
Lab builds one tin
Create Lab.java in the same folder. Its public class line is public class Lab. Inside main, write Tin t = new Tin(7); then System.out.println(t.size);. The argument 7 becomes the constructor parameter size. The constructor stores it with this.size = size;. You do not need a separate t.size = 7; line on this page.
Keep Lab as the class that holds main. Do not put main inside Tin for this lesson. If a leftover Hall file from the constructors page still sits in the folder, do not run java Hall for this lesson — run java Lab after you compile Tin.java and Lab.java.
public class Lab {
public static void main(String[] args) {
Tin t = new Tin(7);
System.out.println(t.size);
}
}
From the folder that holds both files, run javac Tin.java Lab.java. You can compile Tin.java first and Lab.java second when both are complete. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Lab — not java Lab.java, and not java Tin. 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: 7. 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 Tin(7) plus the print in Lab, with this.size = size; in Tin.
Walk the run once. Java starts at Lab.main. The expression new Tin(7) builds a Tin and calls Tin(int size) with the parameter equal to 7. The body writes this.size = size, so the object’s field becomes 7. Then main prints t.size and the screen shows 7.
If you write size = size; instead of this.size = size;, the field stays at 0 for an int. The screen then shows 0, not 7. That is the usual sign that this is missing. Put this. back on the left side of the assignment.
this.size and size side by side
One short table keeps the two names straight while you are inside the constructor.
📊 this.size vs size
| Name | What it means in Tin(int size) |
|---|---|
size alone |
the parameter that entered the constructor |
this.size |
the field on the object being built |
this.size = size; |
store the parameter into the field |
size = size; |
does not update the field |
The table is only a reminder. The typed line you keep is this.size = size; inside the constructor, then Tin t = new Tin(7); and System.out.println(t.size); in Lab. |
Compared with the constructors page
On the constructors page, Box(int n) { size = n; } worked because n and size were different words. That shape still works. This page deliberately uses the same word twice so you need this. A leftover Box and Hall demo with size = n; is a different lesson. For this page keep Tin and Lab and this.size = size;.
Printing t alone with System.out.println(t); does not show a clean 7. Print t.size to see the stored number. The object reference alone is not the field value.
You can still choose a different parameter name on other days. This lesson chooses matching names on purpose. Do not mix n and this in the same Tin demo on this page. One clear path is enough: matching names, this on the field, print the field.
Two tins still stay separate
Each new Tin(...) runs the constructor again for a separate object. You can write Tin a = new Tin(2); and Tin b = new Tin(9);, then print both size fields. Each call uses this.size = size; on its own object. The two objects do not share one size slot. You do not need a second code box for that check — change the numbers in Lab once you already saw 7, or keep the single-tin demo as the file you save.
If both lines show 0, the constructor is still missing this on the left side. If javac cannot find Tin while compiling Lab.java, check that Tin.java sits in the same folder and that the class line is public class Tin.
What this leaves for instance methods
This page uses this only inside the constructor to store a field. A later page — instance methods — shows methods that run on an object and can read or change fields. Stay with the constructor store here. Do not invent extra methods on Tin for this lesson unless you already know them; the lesson goal is this.size = size when names match.
If Lab still has Box b = new Box(4); from the constructors demo and this folder has no matching Box class you intend to keep, remove that line. Keep Tin t = new Tin(7); and System.out.println(t.size);. The compiler will not invent a Box for you when you are teaching Tin.
When the folder still holds Box.java or Hall.java, leave them alone or delete them only if they confuse your compile command. Run java Lab after you compile the Tin and Lab pair from this lesson.
Before instance methods
You can declare int size;, write Tin(int size) { this.size = size; }, and build with Tin t = new Tin(7); from Lab. Compile both files and run java Lab. The screen shows 7. The word this names the object’s field when the parameter uses the same name. You passed the number at build time, the constructor stored it with this, and the print proved the store. The next page is instance methods, where methods on the object use fields after the object already exists.
Keep the constructor header without void. Keep this. on the field. Keep the print on t.size. 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