Loading...

C# static

Write a class Cup with a static field int tally and a static method void Mark() that runs tally = tally + 1;. In Pine, keep Main and call Cup.Mark(); twice, then write Cup.tally. Keep both types in the same teaching file so dotnet run stays simple. Run with dotnet run. The screen should show one line: 2. The method runs through the class name, not through an object variable. Sharing one tally on the class is what this page teaches. Keep the starting public class named Pine.

A member that belongs to the class name

On the instance-methods page you called j.bump() through a jar variable. Here the field and the method sit on Cup itself. You reach them as Cup.Mark() and Cup.tally. There is no new Cup(...) required for that call on this page. Every call to Mark adds one to the same tally.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any Jar j = new Jar(3); leftover if that bump demo is still all you have in Main. Delete any Jar class leftover. Delete any Tin 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 Cup. Inside the braces declare static int tally;. Then write the static method: static void Mark() { tally = tally + 1; }. There is no constructor and no instance field on this page. Save the file before you run.
The method header needs both static and void. The word static marks the member as belonging to the class name. The word void still means Mark does not return a value to write. Keep both words in that order in the header you type.

Cup with static Mark

csharp
using System;

public class Cup
{
    static int tally;

    static void Mark()
    {
        tally = tally + 1;
    }
}

Pine calls Mark on the class name

Pine keeps public static void Main(). Inside Main, write three lines in order: Cup.Mark();, then Cup.Mark(); again, then Console.WriteLine(Cup.tally);. Each call adds one. The write shows the shared count. You are not writing Jar j = new Jar(3); or j.bump(); on this page. That was the instance-methods lesson.

Pine two Marks

csharp
using System;

public class Pine
{
    public static void Main()
    {
        Cup.Mark();
        Cup.Mark();
        Console.WriteLine(Cup.tally);
    }
}

public class Cup
{
    static int tally;

    static void Mark()
    {
        tally = tally + 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 — Main itself is already static, which is why the program can begin without building a Pine object first. The call Cup.Mark() enters Mark on the class. The body writes tally = tally + 1. The first call moves tally from 0 to 1. The second call moves it to 2. Then Main writes Cup.tally and the screen shows 2.
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 the two Cup.Mark(); calls and the write in Pine, with Mark in Cup. The screen shows 2. If the screen shows 4 or 5, a leftover Jar bump demo may still be running. If the screen shows 0, the Mark calls may be missing or may run after the write.
If you write Mark(); with no Cup. in front from this shape of Main, the build refuses unless you add more wiring this page does not teach. Keep Cup.Mark();. If you write Cup c = new Cup(); and then only talk about instance methods again, you left the static lesson. This page’s goal is the class-name call.

Compared with j.bump and with a leftover Jar

On the instance-methods page, j.bump() needed an object in front. That still works when you want a change on one jar. This page’s Cup.Mark() needs the class name in front. A leftover Jar j = new Jar(3); j.bump(); Console.WriteLine(j.fill); is a different lesson. For this page keep Cup and Pine and Cup.Mark();.
You could still invent an instance method later. This lesson keeps the shared counter on the class so two calls without new still share one tally. Writing Console.WriteLine(Cup); alone does not show a clean 2. Write Console.WriteLine(Cup.tally); to see the stored number.

📊 j.bump vs Cup.Mark

Call shape Needs an object? What changes
j.bump(); yes — a jar variable that jar’s fill
Cup.Mark(); no — use the class name the shared Cup.tally

One tally shared by every Mark call

You do not build two cups to get two marks on this page. The first Cup.Mark(); and the second Cup.Mark(); both touch the same static field. That is the point of static int tally. If you later add objects, they still share that one tally unless you invent a different design. For the teaching file, two class-name calls are enough.
If the screen still shows 1, only one Mark call may be present, or a write may sit between the calls. Keep both calls before Console.WriteLine(Cup.tally);. If the screen shows 7 or another leftover number, an older Tin or Jar demo may still be in the file. Keep Cup.Mark(); twice and the write on Cup.tally.

Mistakes that hide the static call

Leaving static off Mark turns it into an instance method. Then Cup.Mark(); from Main fails the build, and the page stops teaching the class-name call. Put static back on both the field and the method. Calling only j.bump(); because the Jar file is still open also hides this lesson — delete that demo from Main when you practice Cup.
Another trap is writing Cup.tally = Cup.tally + 1; only inside Main and never writing Mark. That can print 2 after two assigns, but it skips the static method shape this page wants you to type. Prefer two Cup.Mark(); lines so the method body owns the add. If IntelliSense offers an instance call after new Cup(), ignore it for this exercise and stay with Cup.Mark();.

What this leaves for access

This page reaches members through the class name: Cup.Mark() and Cup.tally. A later page — access — shows words like public and private that control who can see a member. Stay with static here. Do not invent access keywords on every line unless you already use them; the lesson goal is the class-name call and the shared field.
Do not nest Cup inside Pine’s braces. On this page the two classes sit as siblings in the file. If the build cannot find Cup while compiling Pine, check that public class Cup sits in the same file. If the screen shows 0, Mark may never run — keep both Cup.Mark(); lines before the write. If the build complains that Mark needs an object, check that the method header still includes static.

You can declare static int tally;, write static void Mark() { tally = tally + 1; }, and from Pine.Main run Cup.Mark(); twice, then write Cup.tally. Run with dotnet run. The screen shows 2. The static method ran on the class name and changed the shared field. Think of Mark as an action the class itself can do without building a cup first. Pine stays the door you open with dotnet run. The next page is access, where some members are opened or closed to other code on purpose.
Keep static on the field and on the method. Keep the class name before the call: Cup.Mark();. Keep the write on Cup.tally. Those three habits are the checkpoint for this page before you move on. Leave leftover j.bump() demos out of Main so the screen cannot show a jar number by mistake.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 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