473,698 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2057
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.co m

"OutdoorGuy " <Ou********@fis hing.com> wrote in message
news:%2******** *******@tk2msft ngp13.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.GetA ge();
}

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.co m

"OutdoorGuy " <Ou********@fis hing.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.GetA ge();
}

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.GetAge Difference(Empl oyee1, Employee2)

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

Employee1.GetAg eDifference(Emp loyee2)

--

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********@fis hing.com> escribió en el mensaje
news:%2******** ********@TK2MSF TNGP12.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.GetA ge();
}

Thanks.

Sherwood


Nov 17 '05 #5
OutdoorGuy <Ou********@fis hing.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.GetA ge();
}


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.InstanceM ethod();

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********@fis hing.com> wrote in message
news:%2******** *******@tk2msft ngp13.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******** ********@TK2MSF TNGP14.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******** ********@tk2msf tngp13.phx.gbl. ..

"Frisky" <Fr***********@ NorthPole.net> wrote in message
news:uc******** ********@TK2MSF TNGP14.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
4420
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 characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
17
3267
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
5
3746
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 their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
11
3432
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 vtable pointer is made use of.. eg. class one {
2
6110
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 pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim == B.col_dim); for(i=0; i < A.row_dim; i++) for(j=0; j < A.col_dim; j++)
0
2340
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 jobs. When a non-secure page references a secure page with relative URL, the web server generates error until absolute URL with https prefix is used. On the other hand when a secure page references a non-secure page, the non-secure page will be...
399
12822
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 to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
13
18943
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 the Post: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/e81cd9d9c2200d74/48c0774eeb3bd998?hl=en&lnk=gst&q=initial+value+of+reference+to+non-const#48c0774eeb3bd998 ...
9
3800
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
29881
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.