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
193
votes
5 answers

Are static class instances unique to a request or a server in ASP.NET?

On an ASP.NET website, are static classes unique to each web request, or are they instantiated whenever needed and GCed whenever the GC decides to disposed of them? The reason I ask is because I've written some static classes before in C# and the…
Dan Herbert
  • 99,428
  • 48
  • 189
  • 219
193
votes
8 answers

Difference between final static and static final

I found a code where it declared code like private final static String API_RTN_SUCCESS = "0"; private final static String API_RTN_ERROR = "1"; public static final String SHARED_PREFERENCE_CONFIG = "shared_preference_config"; public static final…
MBMJ
  • 5,323
  • 8
  • 32
  • 51
192
votes
23 answers

static constructors in C++? I need to initialize private static objects

I want to have a class with a private static data member (a vector that contains all the characters a-z). In java or C#, I can just make a "static constructor" that will run before I make any instances of the class, and sets up the static data…
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
190
votes
3 answers

Static member initialization in a class template

I'd like to do this: template struct S { ... static double something_relevant = 1.5; }; but I can't since something_relevant is not of integral type. It doesn't depend on T, but existing code depends on it being a static member…
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
189
votes
4 answers

Retrieve only static fields declared in Java class

I have the following class: public class Test { public static int a = 0; public int b = 1; } Is it possible to use reflection to get a list of the static fields only? I'm aware I can get an array of all the fields with…
Anders
  • 1,977
  • 2
  • 11
  • 10
187
votes
20 answers

What is the use of a private static variable in Java?

If a variable is declared as public static varName;, then I can access it from anywhere as ClassName.varName. I am also aware that static members are shared by all instances of a class and are not reallocated in each instance. Is declaring a…
Vaibhav Jani
  • 12,428
  • 10
  • 61
  • 73
186
votes
9 answers

Stack, Static, and Heap in C++

I've searched, but I've not understood very well these three concepts. When do I have to use dynamic allocation (in the heap) and what's its real advantage? What are the problems of static and stack? Could I write an entire application without…
Hai
  • 4,764
  • 8
  • 29
  • 26
185
votes
1 answer

What is the purpose of static keyword in array parameter of function like "char s[static 10]"?

While browsing some source code I came across a function like this: void someFunction(char someArray[static 100]) { // do something cool here } With some experimentation it appears other qualifiers may appear there too: void someFunction(char…
dreamlax
  • 93,976
  • 29
  • 161
  • 209
184
votes
17 answers

How can I get a resource content from a static context?

I want to read strings from an xml file before I do much of anything else like setText on widgets, so how can I do that without an activity object to call getResources() on?
lost baby
  • 3,178
  • 4
  • 32
  • 53
183
votes
7 answers

static function in C

What is the point of making a function static in C?
Cenoc
  • 11,172
  • 21
  • 58
  • 92
179
votes
5 answers

Is it possible to get CMake to build both a static and shared library at the same time?

Same source, all that, just want a static and shared version both. Easy to do?
gct
  • 14,100
  • 15
  • 68
  • 107
178
votes
20 answers

C++ static virtual members?

Is it possible in C++ to have a member function that is both static and virtual? Apparently, there isn't a straightforward way to do it (static virtual member(); is a compile error), but is there at least a way to achieve the same…
cvb
  • 4,321
  • 6
  • 26
  • 19
171
votes
15 answers

Are static methods inherited in Java?

I was reading A Programmer’s Guide to Java™ SCJP Certification by Khalid Mughal. In the Inheritance chapter, it explains that Inheritance of members is closely tied to their declared accessibility. If a superclass member is accessible by its…
Algorithmist
  • 6,657
  • 7
  • 35
  • 49
168
votes
2 answers

How do I use the C#6 "Using static" feature?

I'm having a look at a couple of the new features in C# 6, specifically, "using static". using static is a new kind of using clause that lets you import static members of types directly into scope. (Bottom of the blog post) The idea is as…
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
165
votes
4 answers

Is it OK to use Gson instance as a static field in a model bean (reuse)?

Here's the model I implemented: public class LoginSession { private static final Gson gson = new Gson(); private String id; private String name; private long timestamp; public LoginSession(String id, String name) { …
philipjkim
  • 3,999
  • 7
  • 35
  • 48