7

I've created a class which was static first, this class does not persist state (does not keep context or any variables) is just a list of functions. But the class is not very used in the app so I decided to make class instantiable.

Why?

Because I think an instantiable class would use less memory as it is not available during the whole app life cycle.

Is this right?

A static class uses more memory than a non-static class?

Thank you

1
  • "Static class" is not a standard term. What do you mean by "static class"? In any case the answer is probably "no there is no difference." Commented Oct 25, 2013 at 8:24

2 Answers 2

11

I think you've misunderstood how classes work. Any kind of class is "available" throughout the lifetime of the app. Memory used for the class itself (the methods etc) is very different to memory used by instances. Unless you actually create an instance of the class, it's irrelevant. And even static classes can be instantiated - it's just that they don't maintain an implicit reference to an instance of the enclosing class.

If your class doesn't actually require an implicit reference (i.e. it doesn't use it) then make it a static nested class - or pull it out as a top-level class anyway. (Nested classes can be a pain sometimes - the rules around top-level classes are simpler.)

Sign up to request clarification or add additional context in comments.

Comments

2

A static class as such doesn't use more memory than a non static one. All classes are always available in an application - you can always use a static class or create an instance of a non static one.

If you have only methods in your class (which are of a helper method types) a static class is more convenient to use (no need to create an instance) and won't affect your memory usage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.