C# Instance Methods
Write a class Jar 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 Pine, keep Main and build one jar with Jar j = new Jar(3);, call j.bump();, then write j.fill. Keep both types in the same teaching file so dotnet run stays simple. Run with dotnet run. 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. Keep the starting public class named Pine.
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.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any Tin t = new Tin(7); leftover if that this-page call is still all you have in Main. Delete any Tin class leftover. Delete any ReadLine line. The file you keep has no wait for typing.
Outside Pine’s braces — still in the same file — write public class Jar. 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 on this page. Save the file before you run.
The method header needs void because bump does not return a value to write. The constructor still has no void — keep that difference clear.
using System;
public class Jar
{
int fill;
Jar(int n)
{
fill = n;
}
void bump()
{
fill = fill + 1;
}
}
Pine builds a jar and calls bump
Pine keeps public static void Main(). Inside Main, write three lines in order: Jar j = new Jar(3);, then j.bump();, then Console.WriteLine(j.fill);. The constructor stores 3. The call j.bump() adds one. The write shows the new value. You are not writing Tin t = new Tin(7); on this page. That was the this lesson.
using System;
public class Pine
{
public static void Main()
{
Jar j = new Jar(3);
j.bump();
Console.WriteLine(j.fill);
}
}
public class Jar
{
int fill;
Jar(int n)
{
fill = n;
}
void bump()
{
fill = fill + 1;
}
}
Read the kept file from the top. using System; prepares short names. public class Pine is the starter. public static void Main() is where the runtime starts. 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 writes j.fill and the screen shows 4.
Run it with dotnet run. The program writes one line and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep new Jar(3), j.bump(), and the write in Pine, with bump in Jar. The screen shows 4. If the screen shows 7, a leftover Tin demo is still running. If the screen shows 3, the bump call may be missing or may run after the write.
If you write bump(); with no j. in front, the build 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 Pine 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. Writing Console.WriteLine(j); alone does not show a clean 4. Write Console.WriteLine(j.fill); to see the stored number.
Two bumps on one jar
You can call j.bump(); twice before you write. Start with new Jar(3), bump once to 4, bump again to 5, then write. That check fits in a small second pine demo.
using System;
public class Pine
{
public static void Main()
{
Jar j = new Jar(3);
j.bump();
j.bump();
Console.WriteLine(j.fill);
}
}
public class Jar
{
int fill;
Jar(int n)
{
fill = n;
}
void bump()
{
fill = fill + 1;
}
}
Run again with dotnet run. 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 write 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.
Do not nest Jar inside Pine’s braces. On this page the two classes sit as siblings in the file. If the build cannot find Jar while compiling Pine, check that public class Jar sits in the same file. 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 write.
You can declare int fill;, write Jar(int n) { fill = n; } and void bump() { fill = fill + 1; }, and from Pine.Main run Jar j = new Jar(3);, j.bump();, then write j.fill. Run with dotnet run. The screen shows 4. The instance method ran on that object and changed its field. Think of bump as an action the jar can do after it exists. Pine stays the door you open with dotnet run. 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 write 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