1

If I need to declare just some constants to use in my class, and there is no real need for declaring them as static, i.e. there will no be several instances sharing that member, is there any sense to declare them as static? Is there a "cost difference" between using static and instance members? Please give information about both java and C#.

And also, is there a difference between java's static members and C#'s static members?

vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59
  • 1
    I don't know about Java, but in C#, constants are inherently static ([reference](http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/why-can-t-i-use-code-static-code-and-code-const-code-together.aspx)). This seems intuitive to me; what would be the point of having an instance-level constant? – Joe Farrell Jun 08 '15 at 22:14
  • 2
    As far as I'm aware of in __Java__ static variables are stored in the PermGen space while instance variables are stored on the heap (the PermGen space is a special kind of heap which contains all the loaded blueprints of classes and their methods as well as static variable definitions). Often larger application servers are confronted with this issue, but for small applications this is forgettable. For C# see [this link](http://stackoverflow.com/questions/337019/hows-memory-allocated-for-a-static-variable) – Roman Vottner Jun 08 '15 at 22:22
  • @RomanVottner is that about java or c#? – vcmkrtchyan Jun 08 '15 at 22:23

4 Answers4

1

I need to declare just some constants to use in my class

No Idea about Java but in C# constant member's are implicitly static in nature.

See MSDN Document: Constants (C# Programming Guide) for more information. Quoting from the referred document:

Constants are accessed as if they were static fields because the value of the constant is the same for all instances of the type. You do not use the static keyword to declare them.

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • 1
    Constants are not implicitly static in Java – Vince Jun 08 '15 at 22:27
  • Good to know but have no idea about `Java`. – Rahul Jun 08 '15 at 22:28
  • Yeah I know, I was just clarifying for the "*Not sure about Java*" - The only implicitly static members in Java are fields declared in interfaces (implicitly static final). Edit it into your answer and I'll delete my comments. I wasn't saying whether or not you knew; I was telling you so you could include it into your answer. – Vince Jun 08 '15 at 22:31
  • @VinceEmigh, No worries; feel free to edit my answer anytime(in future) if it value adds. – Rahul Jun 08 '15 at 22:47
1

I generally declare every constant in my applications as a private static final variable, for a number of reasons.

  1. Consistency. It's clear to myself and others what are the default core constant definitions are in my application. I can keep them all grouped together, and it's very easy to navigate over to them if they need to be modified. Because they're static, these changes ripple down through every call. This safegaurds you from making accidentally multiple definitions of the same constant.
  2. Performance. If I have a constant that's never changed, there's no point in constantly allocating memory for it during a method call. It's allocated once when a reference to the class is made, and then you're done with it. There that memory remains references until all references to the class are done with.
  3. Good practice. There's a convention in programming where dependencies of should be as highly decoupled as possible. When you declare a function or variable as static, you enforce that it must be interacted with in a static way, i.e. independently of a runtime instance. Without this dependency of a class encapsulating member variables at runtime, you can more precisely control interaction with that class, because it ensures you define precise rules on how a class can be interacted with. You find that the more constants and methods you can declare as static, the more flexible your software becomes because there's little confusion over how certain values and behaviours are supposed to be manipulated.
  4. Exposure. Some functions or constants you define may be useful to other parts of your application. This means you can take advantage of functionality without the requirement for using a runtime instance. For example, the quadratic formula is the same everywhere, so why should it belong to a single instance at all? The same goes for constants, like the speed of light.

Personally, I always declare all functionality of my application as private static. If it makes sense for a class to use that functionality as part of it's operation, then the programmer can wrap the method call in another method and provide the appropriate member variable data. If another part of your application needs to use that static function, all you need to do is increase the visibility of the function.

Mapsy
  • 4,192
  • 1
  • 37
  • 43
  • Ah, true. Alas, that's where I fail. I'd say that `const` is probably the same as `final`, but I don't know enough about the semantics to provide a precise explanation! – Mapsy Jun 08 '15 at 22:35
  • 1
    In C# you may want to use `private static readonly` or `private const` – Vitali Kotik Jun 08 '15 at 22:37
1

In Java, constants are not implicity static; constants are not an actual language specification, so constants in Java are simply variables that we refer to as constants.

In C#, constants are implicitly static. Constants are part of the language specification, and states in the documentation:

Constants are accessed as if they were static fields because the value of the constant is the same for all instances of the type. You do not use the static keyword to declare them.

Vince
  • 14,470
  • 7
  • 39
  • 84
0

if you will declare constant as static than your program will use only one memory location to store this constant value, and if you will not use static than every object will store this constant separately. As you said there are no several instances, in this case either you specify constant as static or declare it simply both will be of same cost.

Sushant Srivastava
  • 307
  • 4
  • 8
  • 18