Static vs. Non-Static Methods

  • Thread starter Thread starter OutdoorGuy
  • Start date Start date
O

OutdoorGuy

Greetings,

I have a "newbie" question relating to C#. I am still trying to
understand the difference between a static and a non-static method
(particularly when it is called), and was wondering if anyone could
point me to some additional resources? If I see a method call, how can
I be sure that it is Static (or Non-Static)?

Thanks in advance!
 
OutdoorGuy,

A static method does not require an instance of an object to be
executed. A general rule-of-thumb would be to make the method static if you
do not rely on any of the fields that are specific to an instance of an
object.

Hope this helps.
 
Thanks, Nicholas. By that definition, then, the method below(i.e.,
"GetAge()") would be considered a "Non-Static", "Private" method since
an instance of the "Employee" object precedes the call. Am I correct?

public static void Main()
{
Employee myEmployee = new Employee();
int age = myEmployee.GetAge();
}

Thanks.

Sherwood
 
OutdoorGuy,

It would not be static, yes, but it might not be private. If Main is
attached to another class in the same assembly, then GetAge could be
internal or public. However, chances are it is public.

Also, why have a method GetAge? Why not just have an Age property?
This is cleaner, IMO.
 
Yes, in this case, since it is obvious that "age" is specific to each
employee. An example of static method would be:

Employee.GetAgeDifference(Employee1, Employee2)

which also could be converted into a non-static (instance) method as

Employee1.GetAgeDifference(Employee2)

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
OutdoorGuy said:
By that definition, then, the method below(i.e., "GetAge()") would be
considered a "Non-Static", "Private" method since an instance of the
"Employee" object precedes the call. Am I correct?

public static void Main()
{
Employee myEmployee = new Employee();
int age = myEmployee.GetAge();
}

It's a non-static method (in other words, an instance method), yes.

However, it's not private. If it were private then Main wouldn't be able
to call it, since it would be invisible outside of Employee. [*]

[*] This is ignoring a certain scenario involving nested classes in which
private members of one object can be accessed by a different object, but
that's slightly more advanced.
 
Static methods belong to the class. Non-static methods belong to an instance
of a class. So, to call a non-static method, you must create an instance of
the class first. For example,

public class Foo {
public Foo() {}
public void InstanceMethod() {}
public static void ClassMethod() {}
}

To call an instance method, I first need an instance. I create an instance
of a class by creating one with new. Then I can call its method. For
example:

Foo myFoo = new Foo() // create an instance
myFoo.InstanceMethod();

To call a class method, I do not create an instance. I just specify the
class and I can use it. It is globaly available through the class name. For
example:

Foo.ClassMethod();

Note: This brings up an interesting point. Static methods may not access
data in the class except for static data. Since there is no instance of a
class, the data would not exist.

Statics are used a lot for utility classes where you have serveral related
functions. The Math class in .Net is a good example. Another use is for
Factory methods. See the Fatory Method design pattern.

Hope this helps....

Frisky
 
Frisky said:
See the Fatory Method design pattern.

I think I get that the Fatory design pattern sports a .Bloat() method, but
does it also encompass a Memento pattern to handle versioning? Like a
..GoOnDiet() method?

Sorry.. just couldn't help my self. My Bad. =)

- Michael S

ps. Well explained Frisky.
 
Back
Top