By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

static vs. non-static

P: n/a
hi,
this may sound odd and noob like, but what is the 'big' difference
between static and non-static funcitons ?
is there any performace differnce?
what is the best way to use them ?

thnx

mauzi
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> this may sound odd and noob like, but what is the 'big' difference
between static and non-static funcitons ?
is there any performace differnce?
what is the best way to use them ?


For "static" you don't need an instance of the class to use them, you can
just use MyClass.FunctionName();

It makes sense in some circumstances, would you really want to create an
instance of the Math class just to perform some Cosine operations, or would
you prefer just to do Math.Cosine()?

As for members, if you make a member (such as Int32) static it will be
shared amongst all instances of your class (including instances of descended
classes).
--
Pete
-------
http://www.DroopyEyes.com
Audio compression components, DIB Controls, FastStrings

http://www.HowToDoThings.com
Read or write articles on just about anything
Nov 16 '05 #2

P: n/a
Hi Mauzi,

Consider this class

public class MyClass
{
public int nonstaticMember = 0; // this one is unique for every instance
public static int staticMember = 0; // this one is shared between
instances

public MyClass()
{
nonstaticMember++; // increase the unique member ( = 1 )
staticMember++; // increase the static member ( = ? )
// the static member is set to 0 before any instances of MyClass is
created
// so for each new MyClass instance, it will increase
}

// this method can only be accessed through a myclass object
public void NonStaticMethod()
{
Console.WriteLine(nonstaticMember);
Console.WriteLine(staticMember);
}

// this method belongs to the class, not to any particular object
public static void StaticMethod()
{
// static methods cannot access non static methods or members
Console.WriteLine(nonstaticMember); // illegal
Console.WriteLine(staticMember);
}
}

From another class you do this:

MyClass myClassObject1 = new MyClass(); // create an instance of MyClass
MyClass myClassObject2 = new MyClass(); // and another instance
// now nonstaticmember for each object is set to 1
// but static member is set to 1, then to 2;

myClassObject1.NonStaticMethod(); // this works as expected
myClassObject2.NonStaticMethod(); // same method, different instance

// myclass objects can't access static members, they belong to the class
myClassObject1.StaticMethod(); // illegal
myClassObject2.StaticMethod(); // illegal

// the class has access to members and methods that are static
MyClass.staticMember++; // you can access static members through the class
MyClass.StaticMethod(); // as well as static methods
// staticmember is now increased to 3
The result of this code will be
1
2
1
2
3

Non-static methods require an instance (an object) that performs the
method.
Non-static members require an instance to be accessed.

Static methods belong to the class and can only be accessed through the
class
Static members belong to the class but can be accessed through instances
of the class as a shared object

As for performance difference. Well they are two completely different
things.
If you want something to be shared between MyClass objects you typically
make the shared value static. If you want to create some methods
performing stuff that doesn't actually belong to any particular object,
you still have to put them inside a class, but you declare the methods
static. In this case it doesn't really make sense to inherit the class or
make instances of it either. The Math class behaves just like that. It
contains a bunch of static methods and a couple of static members (E and
PI). It also has a a constructor declared private. That way noone can
make instances of it. And the class is 'sealed' making it impossible to
derive from it.
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3

P: n/a
That's why, in C#, the main entry method Main() can be accessed even before
the first instance of the main class has been created.

namespace WindowsApplication1

{

public class Form1:System.Windows.Forms.Form

{

...

static void Main()

{

// Instance of main class created here

Application.Run(new Form1());

}

}

}

regards ,Teis
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.