Static V/s Non Static Methods

P

pintu

Hi All,
Can any one tell me what's the difference between static v/s non
static functions / classes based on performance and memory
requirements.
As much as i came to know that its better to make your classes and
its methods static as much as possible. Is it ok ?
What determines to make a class / Method static or instance type.

Thanks in advance.

Deep.
 
E

Eliyahu Goldin

I don't think there is any difference in performance or memory.

If a method doesn't use any non-static instance members, you can make it
static. You won't need to instantiate an object to use the static method.
Make your decision based on your programming style and design consideration.

For example, String.Empty is a static field. Therefore you can use it like
if (myString == String.Empty)

It could've been made as a non-static property. Then you could've used it
like
if (myString.Empty)

Depends on your preferences.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
A

Aidy

What determines to make a class / Method static or instance type.

If your method works on an instance of an object make it non-static,
otherwise static;

myObject.Number1 = 1;
myObject.Number2 = 2;
myObject.Add(); // non-static
int result = myObject.Result;

int result = myObjectType.Add (1, 2); // static
 
C

Cowboy \(Gregory A. Beamer\)

There is a perf and memory difference, as there is only one instance of the
method loaded in memory, rather than one per object. Whether or not it is a
big difference depends on whether you are using the object anyway, or simply
need the method.


Eliyahu Goldin said:
I don't think there is any difference in performance or memory.

If a method doesn't use any non-static instance members, you can make it
static. You won't need to instantiate an object to use the static method.
Make your decision based on your programming style and design
consideration.

For example, String.Empty is a static field. Therefore you can use it like
if (myString == String.Empty)

It could've been made as a non-static property. Then you could've used it
like
if (myString.Empty)

Depends on your preferences.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


pintu said:
Hi All,
Can any one tell me what's the difference between static v/s non
static functions / classes based on performance and memory
requirements.
As much as i came to know that its better to make your classes and
its methods static as much as possible. Is it ok ?
What determines to make a class / Method static or instance type.

Thanks in advance.

Deep.
 
P

PhilipDaniels

There is a perf and memory difference, as there is only one instance of the
method loaded in memory [for static methods], rather than one per object [for instance methods].

Nonsense.

The code for the method hangs off the runtime type and there is only
one instance of it regardless of whether it is a static or an instance
method.
 
A

active

I can tell you what it means with VB.net.

Suppose you have a class called CTL and it has a method CLEAR that clears
objects of type CTL.

And an object of type CTL called CTL1

Then you might do CTL1.CLEAR and the method would clear CTL1

That would not be a static method since it operated on CTL1 even though CTL1
is not an explicit argument.
BTW. If you call it with: CTL1.LIST a reference to CTL1 is passed as a
hidden argument.

However, suppose CTL1 had a method, LIST that printed "I HERE" on the
screen.
It should be evident that that code has nothing to do with CTL1. It just
happens to be in the class.
It doesn't use CTL1 or modify CTL1 in any way.
That is a good candidate to be a Static method.
In which case you would mark it Static and call it with
CTL.LIST

==================

For a Static variable there is one memory location that is used by all
objects of the class.
Objects can "talk" to each other through it.
Or if it's a Const all objects can reference it.

If it were not marked Static each object would have it's own memory location
and one object would not know the value of another's variable.


hope this helps
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top