Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a static class and a normal class?

Tags:

When should I prefer either a static or a normal class? Or: what is the difference between them?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace staticmethodlar
{
    class Program
    {
        static void Main(string[] args)
        {
            SinifA.method1();
        }
    }

    static class SinifA 
    {
       public static void method1()
        {
            Console.WriteLine("Deneme1");
        }
    }

    public static class SinifB
    {
        public static void method2()
        {
            Console.WriteLine("Deneme2");
        }
    }
    public class sinifC
    {
       public void method3()
        {
            Console.WriteLine("Deneme3");
        }
    }

    public class sinifD : sinifC
    {
        void method4()
        {
            Console.WriteLine("Deneme4");
        }

        sinifC sinifc = new sinifC();  // I need to use it :)
    }
}
like image 910
ALEXALEXIYEV Avatar asked May 28 '10 13:05

ALEXALEXIYEV


People also ask

What is the difference between class and static class in Java?

The major difference between static and non-static class is that: An instance of the static nested class can be created without creating an instance of its outer class. The static and non-static members of an outer class can be accessed by an inner class.

What is the difference between static method and normal method?

A static method is a class method and belongs to the class itself. This means you do not need an instance in order to use a static method. A non-static method is an instance method and belongs to each object that is generated from the class.

What are the difference between a static class and a singleton class?

The difference between the Singleton and Static is Singleton Class can have value when Class object instantiated between server and client, such a way if three client want to have a shared data between them Singleton can be used. Static are always just shared and have no instance but multiple references.

What is a static class?

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new operator to create a variable of the class type.


1 Answers

Static classes contain static objects that can't be instantiated multiple times. Usually what I use static classes for are to house static methods that provide calculations, general processing patterns, string output formats, etc. Static classes are light weight and don't need instantiation.

For instance System.IO.File is a static class with static a method Exists(). You don't create a File object to call it. You invoke it like this

System.IO.File.Exists(filePath)

Rather than doing this

System.IO.File myFile = new System.IO.File(filePath);

if(myFile.Exists()) { /* do work */ }

If you require several objects in software, then you use dynamic classes. For instance if you have an inventory system you may have several Product objects and in that case you would use a dynamic class such as this

public class Product
{

    public int    ProductID   { get; private set; }
    public string ProductName { get; private set; }
    public int    Qty         { get; set; }

    public Product( int productID, string productName, int total )
    {
        this.ProductID = productID;
        this.ProductName = productName;
        this.Qty = total;
    }       
}
like image 130
Jeff LaFay Avatar answered Oct 23 '22 13:10

Jeff LaFay