473,385 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Static vs. Non-Static Methods

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!

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
8 2047
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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!

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4
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" <Ou********@fishing.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Nov 17 '05 #5
OutdoorGuy <Ou********@fishing.com> wrote:
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.
Nov 17 '05 #6
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

"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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!

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #7

"Frisky" <Fr***********@NorthPole.net> wrote in message
news:uc****************@TK2MSFTNGP14.phx.gbl...
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.
Nov 17 '05 #8
Are you saying I'm fat? :)

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
"Michael S" <a@b.c> wrote in message
news:ea****************@tk2msftngp13.phx.gbl...

"Frisky" <Fr***********@NorthPole.net> wrote in message
news:uc****************@TK2MSFTNGP14.phx.gbl...
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.

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
17
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.