When to make a class Static in C# or Java or typescript

Problem:

Still people either beginner or with experience don’t understand clearly when to exactly make a class static or when to use a static class.

Solution:

We’ll see when we should make a class static or when to use a static class and what it means using example a multiplex movie theatre

In the above image we can see diagram of a multiplex movie theatre, it has 5 theaters and one ticket counter. Now we all know people who want to watch a movie need to buy a ticket.

So now we’ll try to match-up this whole concept with a C# project, where  Ticket-Counter has one objective which is to provide tickets for seats in the theatre and Theatre is a class with properties like seats and screen.

So to make a collection of of theatre we’ll have a List of Theatre class like this

List<Theatre> multiplex = new List<Theatre>

So, what should be type of the class Theatre? Should it be static?

As we need multiple theatres for our multiplex it makes no sense to make theatre a static class.

A static class is a class which is supposed to be act like a toolbox/utility/storeroom for an application. It should exist even when there is no-one using it.

Quoting the famous Jon Skeet here, 
Static effectively means “associated with a type instead of any one instance of the type”. So there’s one set of static variables for a type (within an AppDomain) whether you have 0 instances or a million; you don’t need an instance to access a static member, etc.

Let me explain this:

It says the type Ticket-Counter should have business with the type Theatre so either one object of theatre is created with 10 seats oo 5 objects of Theatre are created with 50 seats the business or the association of Ticket-Counter and theatre remains the same and also, even if no one is using the theatre then still the Ticket-Counter remains at its position.

So, it means Ticket-Counters are as a tool to be used by anyone who wants to go to any theatre. Ticket-Counter also acts a storeroom of information of how many tickets are sold, it also keeps name movies presently being screened, etc.

So, you got the answer?

Here Ticket-Counter should be a static class.

So, in any situation where we need a class which act like a utility/toolbox/storeroom of common data across the application we should make it static. 

Here the code for our TicketCounter class and the method gets us tickets. See the ticketCount is increasing, if TicketCounter were a normal class and we were creating objects of TicketCounter class then every object would have ticketCount same i.e. 0

  static class TicketCounter
  {
      static int ticketCount;

      static TicketCounter() {
          Console.WriteLine("TicketCounter contructor called");
      }

      public static int GiveTicket()
      {
         Console.WriteLine("Giving ticket number --> " + ticketCount);
         ticketCount++;
         return ticketCount;
      }
  }
    class Program
    {
        public delegate int DisplayNumber();

        static void Main(string[] args)
        {
            int ticket1 = TicketCounter.GiveTicket();
            int ticket2 = TicketCounter.GiveTicket();
            int ticket3 = TicketCounter.GiveTicket();
            Console.ReadLine();
        }
    }

Below is the output for this program:

From the above image we can see, if you will call the method TicketCounter.GiveTicket() the constructor of the class TicketCounter will be called only for the first call only, in the next call to method GiveTicket() the constructor of the class TicketCounter will not be called, which essentially means we are using the same object which was created during the first call to method TicketCounter.GiveTicket().

So now you understood why we say we don’t need to create object of static class and we can call its method directly.

Read more from Asp.Net C#:

Natalie Harris
With references from Jon Skeet
3.2k
Christopher Palmer
With references from Eric Nat
132
Christopher Palmer
With references from Peter Andrew
244
Natalie Harris
With references from Jon Skeet
290
Christopher Palmer
With references from Eric Nat
3.4k

Leave a Reply

Your email address will not be published. Required fields are marked *