Best practice - Static methods Local constants

Hi,

I have a question hoping someone could shed some lights on to help me understand statics more. I’m hoping I posted in the right forum section.

Let’s say I have 1000 classes, only about 50 of them gets instantiated when playing a level. One day I need to get a bunch of private constant variables, let’s say about 5 ints, 2 doubles, 2 floats, 1 double (randomly made up), in those 1000 classes to create some sort of large UI containing those data.

If I manually decide to add ONE static method which returns those 10 private constant variables in every class to retrieve the data:

The lifetime of the private constant variables is the duration of the method call, correct?
The lifetime of all those newly created 1000 static methods will now exist throughout my application, correct?
Should I be worried in terms of performance if this happens even though they’re only accessed by one class, during the UI?
What’s some obvious consequences? Memory usage or anything like that?
What would you do if you want to retrieve 1000 classes worth of data private const variables, offline (no internet connection), for a single player game? (I assume most will think about parsing some kind of .txt, .xml)

Very much appreciated, thank you.

Well, your question lacks proper terminology and confuses me very much. The term “constant variable” is like "white blackness’. There are constants and there are variables. And there are many kinds of variables with different scopes and lifetimes: static fields, instance fields, method parameters, local variables. Also variables can be captured in closures that may affect their lifetime and the lifetime of the objects they store unpredictably.

If you talk about local variables themselves, then I would say generally yes. If you talk about actual objects/values that these variables store, then the general answer is no. If you talk about private or other kinds of fields or the objects they store, then the answer is also no.

Don’t call it a lifetime. You can call static methods whenever you want from wherever you want (unless the static constructor has thrown an exception and the whole type hasn’t been initialized properly.)

You shouldn’t. Methods don’t affect performance unless you actually call them.

Nothing special.

It would be easier if you describe what are all these values are supposed to be used for.

Hi alexzzzz,

I really appreciate this, so thanks for answering my questions. I think I understand the concept of statics now and it’s definitely more clear now.

What I meant by private constant variables:

private const int MAX_HEALTH = 5; // private const variable (this is what I mean, sorry)
private const int ...

public static const int MaxHealth
{
    get { return MAX_HEALTH; }
}

Something like that. But you pretty much answered everything already. I guess the last question you could simply imagine a scenario where a client, let’s say employer to avoid confusion, has hired you to create an encyclopedia UI. Unfortunately, all the data that your employer requires you to display is inside those 1000 classes, uniquely defined.

Example:

public class Ogre : MonoBehaviour
{
    private const int MAX_HEALTH = 1;
}

public class Starfish : MonoBehaviour
{
    private const int MAX_HEALTH = 5;
}

public class FlyingBird : MonoBehaviour
{
    private const int MAX_HEALTH = 555;
}

....

Yep.