Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, March 25, 2017

What is Static Class and Static Members


Static Class
We want implementation simpler and faster and do not want to create an object in order to call methods of Class.We make this class as Static class.Static class used to define the information that is object independent. 
By static class, we achieve rule of enforcement.we make class as static to ensure everything as static.Information of these class is primarily made for each.
A class can be declared static. There are two key features of a static class. First, no object of a static class can be created. Second, a static class must contain only static members.
Within the class, all members must be explicitly specified as static. Making a class static does not automatically make its members static. 
The main features of a static class are: 
They only contain static members. 
They cannot be instantiated. 
They are sealed. 
They cannot contain Instance Constructors as we can not create object of class.(Instance constructors are used to create and initialize instances. The class constructor is invoked when you create a new object)

static classes have two primary uses. 
 a) static class is required when creating an extension method. Extension methods relate mostly to LINQ, 
 b) static class is used to contain a collection of related static methods.


The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited.

Static Members
If any information is common to all object,mark as static and keep it in class location means it becomes part of class location and we do not need object of class to access it.
if any member is preceded by static is part of class.only one copy of that member is created for entire class no matter how many objects are created.it is visible within class but lifetime is entire program.

Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside of class definition and also inside the class definition.

static member is also known as property of class.it stays in class location.static member retain values b/w called and caller.
it exist in class location they can retain values for every object of class.When a member is declared static, it can be accessed before any objects of its class are created and without reference to any object. You can declare both methods and variables to be static.If any object of the class are created, they cannot be used to access the static member.
Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member.
Real time Example
Notice board - It is common to all students of college. So information is static member and notice board is static class.
Step 1 - We have created static class "Vehicles", static member "NoOfWheels" and static method "Model". here we have initiliazed the value of static member within class.

public static class Vehicles
    {
       public static int NoOfWheels = 4;
       public static string Model()
        {
            return "Model is Tata Safari LX";
        }

    }

Step 2 - To access static member "NoOfWheels" and static method "Model", we do not have to create object of class.we directly can access by class name.

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("No of wheels is " + Vehicles.NoOfWheels);
            Console.WriteLine(Vehicles.Model());
            Console.ReadLine();
        }

    }

Output


























We can also initialize value of Static member outside class.
Step 1
 public static class Vehicles
    {
       public static int NoOfWheels;
       public static string Model()
        {
            return "Model is Tata Safari LX";
        }
    }

Step 2
 class Program
    {
        static void Main(string[] args)
        {
            Vehicles.NoOfWheels = 5;
            Console.WriteLine("No of wheels is " + Vehicles.NoOfWheels);
            Console.WriteLine(Vehicles.Model());
            Console.ReadLine();
        }
    }

Output


No comments :

Post a Comment