Hello,

I'm just wondering about static and non-static methods. I have a basic understand that non-static methods require a new object to be initialized to be used such as:

public static void main()
{
       mainProgram main = new mainProgram();
       main.doNothing();
}
public void doNothing()
{
}
]

V.S.

public static void main()
{
     doNothing();
}
public static void doNothing()
{
}
]

But I don't understand the context in which they should be used. When should a static method be used over a non static method and vice versa?? I don't really see much of a difference and I have written programs in both methods but want to learn the "correct" way of utilizing these methods.

I have some understanding of static versus non static. But your code looks rather strange, to say the least:-O ??? Just as a starter, what is mainprogram supposed to mean?

Static methods are just global functions that could very well have nothing to do with the class you've put them in. Methods that are not static are merely functions that, in their definition, implicitly take the object they're called on as the first parameter.

Seems you have almost answered your own question really. you obviously understand the difference. There is no right or wrong way to choose.

Static methods shorten code, typically a static method creates an object does some thing with it and returns a value, requiring a simple interface for a complicated task, but its certainly not limited to that.


an Danny is right, your first code block does look very strange, you can't instantiate an object from inside the object itself. but Im sure it was just a mock code for your point.

still. its entirely incorrect.

Sorry, ya it just some pseudo code I whipped up. mainProgram is the mainProgram.cs file where the code rests in. I probably should have made the instance of mainProgram.cs something other than main.

Given that you already understand that static methods are "class" methods and non-static methods are "instance" methods, when to use them is a matter of preference more than a need to... Usually, you will define instance type methods within your class implementation because you intend to operate on an instance object of the class. Sometimes, there are some good reasons to create static methods though--for example:

Testing the class design:

Suppose you have just coded your class design and you want to test it for bugs. You can create a static method called public static void Test() or something. Inside Test, you could instantiate an object of your class, initialize variables to values that mimick how the class is used, and call each of the instance methods to ensure they are working/bug-free. Of course, you could do this without a static method by instantiating a class object in your form's code or similar, but this might result in placing a bunch of code in your form's implementation that you don't really need for the form, and also require you to run form code that might contain it's own bugs and interfere with isolated testing of your class object. Then, when you are done testing, what will you do with all the test code you wrote? Using static method(s) for testing this way encapsulates unit-testing into the class's design, which makes good sense/practice.

Code/Design Organization:

Suppose you have a class that operates on bitmap or image objects and you realize there are some methods that can be reused by other parts of your application. For instance, your image object class sometimes needs to convert an Image to a byte array:

public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

Because the method is defined as static (class method), it's usage is exposed without having to create an instance of your class object. Of course, you could just create a class called MiscMethods or something and put all of your static methods inside it, but it probably makes sense to organize your code by putting the method inside your image related class.

Actually, I have a class called MiscMethods where I will put a new static method when I need one and it doesn't make sense for it to be in my object's class, and I don't yet have enough need to create a separate class. Later, when the need becomes clearer, I will periodically move these static methods into respective/related classes for better organization of my libraries.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.