Monday 9 February 2015

Static Members vs Instance Members

Introduction
Static and Instance declaration was something that I found difficult to get my mind around when I first began programming and, after some searching, realized that there are not many places on the Internet that explain it in depth (or at least not to the point that makes any sense!).

Background
I recently had the opportunity to do some Technical Editing for one of Wrox Publishing's upcoming books and had placed quite a lengthy comment in the manuscript related to Static vs Instance members. Unfortunately it was much too long for a side note and, at the suggestion of the author, I have decided to place that comment here, albeit expanded.

Using the Code
This code is functional in the aspect that it gets the point across of using Static and Instance members but it is up to you to expand upon this theory to make the code useful for anything. First, we need to understand the difference between static and instance members:
  1. Static: Static members are shared with all objects of the class.
  2. Instance: Instance members belong to the object that they are instantiated from.
  • In Non Static class, we can also have static constructor, static member and static functions. 
  • In Static class, we can have only static constructor, static member and static functions. 
While this may sound somewhat cryptic, it will all become much clearer with the following code:

public class CloneGenerator

   {
       //the total number of clones that we create with this class
       private static int numberOfClones;

       //each clone will have a name
       private string cloneName;

       //constructor
       public CloneGenerator(string s)
       {
           cloneName = s;
           numberOfClones++;
           return;
       }

       // return this clones name
       public string getName()
       {
           return cloneName;
       }

       //count Clones
       public static int countClones()
       {
           return numberOfClones;
       }

   }

   public class GenerateClones
   {
       public void main()
       {
           //Instantiate our class and create a clone
           CloneGenerator c1 = new CloneGenerator("Bob");
           CloneGenerator c2 = new CloneGenerator("Jon");
           CloneGenerator c3 = new CloneGenerator("Jim");

           //this string will contain the name Bob
           string clone1 = c1.getName();

           //this string will contain the name Jon
           string clone2 = c2.getName();

           //this string will contain the name Jim
           string clone3 = c3.getName();

           //Call the Static function
           //iCloneCount will hold the value of 3 after this assignment
           int iCloneCount = CloneGenerator.countClones();
       }
   }

As you can see, we instantiate three CloneGenerator objects and pass in a string parameter, which contains the clone's name, and then assign those values to local string variables by calling [objectName].getName()which returns the name of that cloneGenerator object. In contrast, we populate the value of iCloneCountby calling our static function directly: CloneGenerator.countClones() which will return 3

To further explain, if you were to do something such as this...

string name = CloneGenerator.getName();

... you will receive a run time error to the effect of "Method call requires an instance of an object" since getNameis an instance member. One thing to quickly note is that you CAN access static members inside of Instance members (in the above example, we reference the static member numberOfClones inside the instance member CloneGenerator(string s)) but you can NOT access Instance members inside of static members.

So, in summary, an Instance member is a member that belongs to an Instance of an object (in our example, the objects are c1c2, and c3) whereas a static member belongs to the class itself and does not require an instance of an object as was demonstrated by making a call to CloneGenerator.countClones().

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives