5,666,132 members and growing! (18,059 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: A Public Domain dedication

Static Members vs Instance Members (Overview)

By Douglas Parsons

An overview on how Static and Instance Declaration works
C# 1.0, C# 2.0, C#, Windows, .NET, .NET 1.0, .NET 1.1, .NET 2.0VS.NET2002, VS.NET2003, VS2005, Visual Studio, Dev

Posted: 18 Jun 2007
Updated: 18 Jun 2007
Views: 6,866
Bookmarked: 5 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 3.90 Rating: 3.05 out of 5
6 votes, 31.6%
1
4 votes, 21.1%
2
1 vote, 5.3%
3
5 votes, 26.3%
4
3 votes, 15.8%
5

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:

  • Static: Static members are shared with all objects of the class.
  • Instance: Instance members belong to the object that they are instantiated from.

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 iCloneCount by 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 getName is 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 c1, c2, 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().

Points of Interest

I probably would not have written this article without the suggestion from the author who I worked with at Wrox so I must give him his due credit for suggesting I do this. Hop on over and take a look at his books and his site in general!

If someone has a suggestion to make this more understandable or something that will drive the point home more efficiently than I have done, please shoot me an email.

History

  • 18th June, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

Douglas Parsons


Nothing much to say, I am 25 year old Computer Programmer from Akron, Ohio; nothing to see here, move along. =P
Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralGood for noobies - "bad" for prosmemberUrs Enzler21:21 25 Jun '07  
GeneralAnother viewmemberTom Spink21:16 25 Jun '07  
GeneralGood for newbies.memberNickolay Karnaukhov22:16 18 Jun '07  
GeneralRe: Good for newbies.memberDouglas Parsons13:31 19 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jun 2007
Editor: Deeksha Shenoy
Copyright 2007 by Douglas Parsons
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project