Static Methods

J

Jay

Why are there static methods in C#. In C++ static was applied to data only
(I believe) and it meant that the static piece of data was not a part of the
object but only a part of the class (one copy of data as opposed to multiple
instances of the class)

C# is now applying the static concept to methods. Why?, I thought that only
one copy of the methods were loaded into memory not matter how many
instances were created. Is this different in C#? A static method seems to
mean that the method cannot access any variable or method outside of the
object.

When static is applied to data, if methods access the private data, the data
is required to be static.

Please explain Static vs Private. Thank You all so much!

Have a happy 4th!
 
G

Guest

Hi Jay,

Don't get mix up.

Private is an access modifier.

Static (method) is a keyword means you can call directly from the class level, and does not require an object variable.

Example:

Common:

Example a = new Example();
a.CallFunc();

With Static:

Example.CallFunc();

Cheers. Correct me if i am wrong.
 
D

Daniel O'Connell [C# MVP]

Jay said:
Why are there static methods in C#. In C++ static was applied to data
only
(I believe) and it meant that the static piece of data was not a part of
the
object but only a part of the class (one copy of data as opposed to
multiple
instances of the class)

Best I can tell, static methods existed in C++, although I'm not really sure
of the semantics of them. The simplist explination I can find is that they
can only operate on static members.
C# is now applying the static concept to methods. Why?, I thought that
only
one copy of the methods were loaded into memory not matter how many
instances were created. Is this different in C#? A static method seems
to
mean that the method cannot access any variable or method outside of the
object.

Methods being static has nothing to do with method loading, but everything
to do with scoping. A static method is one which doesn't belong on an
instance(and in C# cannot be called through an instance), but would
logically belong to the type. By belonging to the type these methods can
exist conceptually where an instance method would not. For example, take
Int32.Parse. This method takes a string and returns an integer parsed from
that string, this particular operation has *no* bearing on the behaviour of
Int32 itself, however it is a method which is logically tied to Int32 *and*
that has no sideeffects, making it perfect for static(or in C++ you might
write a global method here, if you are taken to such things).
This is not, of course, to say that it isn't possible to devise instance
methods that perform the work of a static method, it is just often not worth
it. Statics are clearly associated with the type and require no extra
instances.

Static does apply to data loading, however. Static data members only exist
once per appdomain, as a result, static methods exist with one singular
state instead of an instance specific state which is what instance methods
exist with.
When static is applied to data, if methods access the private data, the
data
is required to be static.
I don't understand what you mean here. Any members can access static data,
but static methods can only access other static methods(or instances of
objects it itself creates).
Please explain Static vs Private. Thank You all so much!
These have two different meanings. Private is an accessibility specifier
that determines who can access a member(the class itself(private), class
inheritors(protected), other classes in this assembly(internal) or
everyone(public)). Static on the other hand specifies that the method is not
associated with an instance, but is instead only associated with the type.

Simply put:
private means that the data is private to the class
static means that the data belongs to the type
private static means that the data is private to the type
 
B

Bob Powell [MVP]

Static methods do exist in C++. The usage rules are identical.

A static method may not access any instance data so it can only work on
variables that are (a) handed to it as a parameter or (b) entirely within
the scope of the method, ie on the stack or (c) static members of the same
or some other class.

An instance method is one which requires an instance of a class to exist
before it can run. Instance data is the non-static data in a class and each
instance of the class will have different instances of the data.

Example..

public class foo
{
static int anInt;
int anOtherInt;

void doo()
{
int x=poo(10); //VALID instance methods may call static methods
anInt=20; // VALID instance methods may access static data
}

static int poo(int woo)
{
anInt=woo; //VALID a static method may access static data
anOtherInt=woo; //EROR a static method cannot accesss instance data
doo(); //ERROR a static method may not access instance methods
int local=20;
local*=10; //VALID a static method may access variables within its
scope
}

}


The private keyword refers to access or visibility of members and classes
and bears no relation to the static keyword.

A private data member or method may only be accessed from the declaring
class. Derived classes may not access the data or member.



--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top