Questions tagged [static]

Static is a term used in some programming languages to define a function or data storage area (field) that is not bound to any specific object instance. In the majority of cases this tag, if used, should be used in conjunction with a specific programming language tag.

Static functions may be defined within the context of a type but the static function can be called without having an instance of the type.

Static fields may be defined within the context of a type, but the storage location for that field is not part of memory allocated to each instance of that type. The static field is allocated from a global storage area.

In C# and C++, a static constructor executes initialization code before the first use of the type and is only executed once for the lifetime of the process. This is different from a normal constructor which initializes a new instance of the class and executes for every new instance of the type.

The word "static" means "unchanging" in other contexts but not in this one: the contents of static fields can usually be modified at runtime. In this context it means "standing still", from the / term meaning that the storage location (memory address) of a static field is calculated at link time and never changes at runtime, so it appears in the object code as a constant. This is different from an instance field whose address is relative to the start of each object instance's memory block, which will be different for each object instance.

Other usage of the term static might refer to any relatively constant data. For example: in information retrieval, the output of may be referred to as the static score of a page, which will provide a boost to the dynamic score the page will get from a different algorithm.

16364 questions
292
votes
15 answers

Getting the class name from a static method in Java

How can one get the name of the class from a static method in that class. For example public class MyClass { public static String getClassName() { String name = ????; // what goes here so the string "MyClass" is returned return…
Miles D
  • 7,960
  • 5
  • 34
  • 35
287
votes
9 answers

Volatile vs Static in Java

Is it correct to say that static means one copy of the value for all objects and volatile means one copy of the value for all threads? Anyway a static variable value is also going to be one value for all threads, then why should we go for volatile?
Jothi
  • 14,720
  • 22
  • 68
  • 93
284
votes
4 answers

ReSharper warns: "Static field in generic type"

public class EnumRouteConstraint : IRouteConstraint where T : struct { private static readonly Lazy> _enumNames; // <-- static EnumRouteConstraint() { if (!typeof(T).IsEnum) { throw new…
bevacqua
  • 47,502
  • 56
  • 171
  • 285
281
votes
3 answers

Does static constexpr variable inside a function make sense?

If I have a variable inside a function (say, a large array), does it make sense to declare it both static and constexpr? constexpr guarantees that the array is created at compile time, so would the static be useless? void f() { static constexpr…
David Stone
  • 26,872
  • 14
  • 68
  • 84
279
votes
11 answers

Should a "static final Logger" be declared in UPPER-CASE?

In Java, static final variables are constants and the convention is that they should be in upper-case. However, I have seen that most people declare loggers in lower-case which comes up as a violation in PMD. e.g: private static final Logger logger…
dogbane
  • 266,786
  • 75
  • 396
  • 414
246
votes
13 answers

Why can't I inherit static classes?

I have several classes that do not really need any state. From the organizational point of view, I would like to put them into hierarchy. But it seems I can't declare inheritance for static classes. Something like that: public static class…
User
  • 30,403
  • 22
  • 79
  • 107
241
votes
11 answers

Why would a static nested interface be used in Java?

I have just found a static nested interface in our code-base. class Foo { public static interface Bar { /* snip */ } /* snip */ } I have never seen this before. The original developer is out of reach. Therefore I have to ask…
Mo.
  • 15,033
  • 14
  • 47
  • 57
238
votes
5 answers

Invoking a static method using reflection

I want to invoke the main method which is static. I got the object of type Class, but I am not able to create an instance of that class and also not able to invoke the static method main.
Steven
  • 3,962
  • 6
  • 21
  • 29
235
votes
14 answers

Static nested class in Java, why?

I was looking at the Java code for LinkedList and noticed that it made use of a static nested class, Entry. public class LinkedList ... { ... private static class Entry { ... } } What is the reason for using a static nested class, rather…
David Turner
  • 5,014
  • 6
  • 26
  • 27
224
votes
20 answers

Should private helper methods be static if they can be static

Let's say I have a class designed to be instantiated. I have several private "helper" methods inside the class that do not require access to any of the class members, and operate solely on their arguments, returning a result. public class Example { …
avalys
  • 3,662
  • 4
  • 25
  • 22
222
votes
3 answers

Call static methods from regular ES6 class methods

What's the standard way to call static methods? I can think of using constructor or using the name of the class itself, I don't like the latter since it doesn't feel necessary. Is the former the recommended way, or is there something else? Here's a…
simonzack
  • 19,729
  • 13
  • 73
  • 118
218
votes
20 answers

Are fluid websites worth making anymore?

I'm making a website now and I am trying to decide if I should make it fluid or not. Fixed width websites are much easier to make and also much easier to make them appear consistent. To be honest though, I personally prefer looking at fluid websites…
Adam
  • 2,606
  • 4
  • 19
  • 14
213
votes
4 answers

Static variables in member functions

Can someone please explain how static variables in member functions work in C++. Given the following class: class A { void foo() { static int i; i++; } } If I declare multiple instances of A, does calling foo() on one instance…
monofonik
  • 2,735
  • 3
  • 20
  • 17
208
votes
15 answers

TypeScript static classes

I wanted to move to TypeScript from traditional JS because I like the C#-like syntax. My problem is that I can't find out how to declare static classes in TypeScript. In C#, I often use static classes to organize variables and methods, putting them…
Rayjax
  • 7,494
  • 11
  • 56
  • 82
197
votes
9 answers

What's a "static method" in C#?

What does it mean when you add the static keyword to a method? public static void doSomething(){ //Well, do something! } Can you add the static keyword to class? What would it mean then?
Moshe
  • 57,511
  • 78
  • 272
  • 425