Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

For example I have these two classes:

First

public class Example
{
    public Example()
    {
    }

    public int ReturnSomething
    { 
      return 1;
    }
}

Second

public class Example
    {
        public Example()
        {
        }

        public static int ReturnSomething
        { 
          return 1;
        }
    }

I want to use the ReturnSomething method, which of the two classes allocated less/more memory? When I use

int x = Example.ReturnSomething;

or when I use

Example object = new Example();
int x = object.ReturnSomething();
share|improve this question
    
Another thing worth considering would be unit testing; static classes are hard to mock (although you may not need to mock a static method to unit test it, it would depend on your exact situation). –  Daniel B May 29 '13 at 6:26

2 Answers 2

up vote 7 down vote accepted

If you don't use the object inside a method (i.e. you don't have this.), then make it static. In .NET Framework, for example, in order to be Code analysis compliant, you have to use static in this case (see rule CA1822: Mark members as static).

Also consider OOP in order for your code to be readable. Is the method inherent to the instance of the object? Is it more appropriate for class only, but not the instance? Is the method appropriate here in the first place?

Note: you're expected to write code for humans, not computers. Unless you work on embedded hardware, write your code with the consideration of readability, not memory consumption. If you're instead in a case where every byte of memory counts, profile the application to determine which of multiple ways of solving the problem matches the non-functional requirements related to memory consumption.

share|improve this answer
    
I'm currently working on a small gameserver. I guess memory conservation is a must because as my server is running, it consumes larger memory. –  greggianbayocboc May 29 '13 at 5:27
    
I guess, I would have to use both depending on the type of functionality i want to use it. Thanks for answering. –  greggianbayocboc May 29 '13 at 5:31

MainMa provided a great answer to your unspoken question ("Which option should I choose"), but, for completeness, I thought I'd answer your explicit question:

I want to use the ReturnSomething method, which of the two classes allocated less/more memory?

Your second option allocates a tiny bit more memory. 12 bytes, to be precise, if you are running an x86 application with .NET 4. However, your object will be garbage collected as needed (unless you actively do something to prevent that), so this becomes irrelevant in the long run (even on very memory-constrained devices).

Unless you have a very good reason for it, this tiny amount should not influence your decision. There are very good reasons to choose the static option, but this is not one of them.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.