Link to home
Start Free TrialLog in
Avatar of dei1c3
dei1c3

asked on

Advantages/disadvantages of Static vs Non-static methods??

Hello experts,

I'm finishing up my degree in Software Development and recently started a job as a Junior Developer.  One of the first tasks I was assigned was to create a data access tier for my part of the project.  No problem, my education covered this type of thing in relative detail.  So I go to work, get it up and running and am pretty happy with the results.  My code involves instantiating a data access object and using it's methods to interface with the database.  Pretty standard, right?  But later, I had the opportunity to look at my Lead's code for the data access tier for his part of the project.  His code looks very similar to mine except that he has used static methods to achieve the same results.

Which got me thinking...what are the advantages and disadvantages of each approach (either for this specific task or in general)?  Is one slower/faster?  Have less/more overhead?  Are there methodology or design-related reasons for using one or the other?

I realize this is kind of a general question, but I'm curious what people have to say about it.  The question is worth 500 points and I'll probably split them up among the 1-3 anwers I think contribute the most to my understanding of this issue.
Avatar of sachiek
sachiek
Flag of Singapore image

Well, First thing I would like to share is dont limit your knowledge.

Always it is good to learn that there are more than one single way to acheive best results.

Since you have joined recently, you should be soon realizing that writing code is always been easy part of as a software developer. But controling emotions , then comparing this kind of coding all these trigger things much.

So better be patient and try to learn more.

Just my two cents... Dont mistake me if any of my comments is wrong.


Sachi
Avatar of AlexFM
AlexFM

Static and non-static methods are used in different situations. Methods related to class instance should be non-static. Method related to the whole class and not to it's specific instance should be static.
Example: .NET Color structure. B property is non-static because it gets/sets blue component of specific Color instance:
Color c;
...
c.B = 255;

Blue is static property. It is not related to any class instance and always returns the same value:
Color c = Color.Blue;
>>Methods related to class instance should be non-static. Method related to the whole class and not to it's specific instance should be static.

I will add few lines for example.

You have a class as Customer.
So in this class you can write a static method
public static Customer GetCustomer()
Which loads the data in Customer object and returns the object.


Static methods are useful for passing around the same instance of a given class between many others in the same process, without explicity passing the reference. Take for example a counter class:

public class Counter
{
  //Member Vars.
  private static int mCounter = 0;

  //Constructor.
  private Counter(){} //To ensure this class is not instanced.

  //Methods
  public static int GetCounter(){ return mCounter; }
  public static void IncCounter() { mCounter++; }
  public static void DecCounter() { mCounter--; }
}

If I have 2 classes in the same process and each one calls Counter.GetCounter(); they will get the same value, equally, incrementing or decrementing the counter by one of the classes will affect the other two classes.

public static Main()
{
  MyClass mC1 = new MyClass();
  MyClass mC2 = new MyClass();

  mC1.Print(); //Output = '0'
  mC2.Print(); //Output = '0'
  mC1.Inc();   //mC1 increments causing mC2 to see the increment as well.
  mC1.Print(); //Output = '1'
  mC2.Print(); //Output = '1'
  mC2.Dec();  //mC2 decrements causing mC1 to see the decrement as well.
  mC1.Print(); //Output = '0'
  mC2.Print(); //Output = '0'
}

public class MyClass{
  public void Print(){ Console.WriteLine(Counter.GetCounter()); }
  public void Inc(){ Counter.IncCounter(); }
  public void Dec(){ Counter.DecCounter(); }
}
   
Cheers
Wint.
ASKER CERTIFIED SOLUTION
Avatar of RichardLinnell
RichardLinnell

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I only use static methods in my Utility Class so that I can access directly without create an instance.
For example:
public class Util
{
   public static byte[] encrypt (string strSource) {}
   public static string decrypt (byte[] bData) {}
   public static void blsadj() {}
   public static int kdasjd() {}
}

If just in case I only need to use encrypt and decryp all I have to do is
bData = Util.encrypt("Hello World!");


I think the text book answer for this is that Static methods work at a Class level and non-static at an instance/Object level.

For these utility functions where it is the service that is important and the Class doesn't have any data attributes of its own then a Static Method can provide the service without the overhead of having to instantiate the object.
You can see alot of these types of services supplied by Static methods in the Conversion functions.

In terms of performance.
It can also remove the memory overhead of having multiple "Utility Objects" hanging around waiting for some work.
There is an overhead in that the Class does get instantiated ( I belive ) for the duration of the call - but as most of these Classes have little or no data footprint that is not such a big thing.

Cheers
Jon

Avatar of dei1c3

ASKER

Oh my gosh...I completely forgot about this question.  I posted it one day at work and forgot to check it when I got home later.  Apologies for the late response.

Anyway, I accepted the answer that I think best answered the question I actually asked.