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
8
votes
4 answers

How did it happen that "static" denotes a function/variable without external linkage in C and C++?

In C static can mean either a local variable or a global function/variable without external linkage. In C++ it can also mean a per-class member variable or member function. Is there any reference to how it happened that the static keyword that seems…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
8
votes
5 answers

iterate static int values in java

I have a simple question. Is there a way ( using reflections I suppose ) to iterate all the static values of a class? For instance class Any { static int one = 1; static int two = 2; static int three = 3; public static void main(…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
8
votes
2 answers

Django - serving and managing permissions for static content

I have certain documents that I want to limit access to through Django to authorized users with permissions only. If I'm going to use Django as a proxy to serve static files in a view, what are the implications? I'm used to serving static files in…
Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114
8
votes
1 answer

How do I declare an instance of one of my Rust structs as static?

How do I declare an instance of one of my own structs as static? This sample doesn't compile: static SERVER: Server<'static> = Server::new(); fn main() { SERVER.start("127.0.0.1", 23); }
Bruce
  • 461
  • 1
  • 8
  • 17
8
votes
1 answer

static:: vs. self:: - are there any downsides?

In this StackOverflow question I learned that self:: was not inheritance-aware where static:: was (in PHP). When it comes to defining a bunch of constants within a class, if you want to override those constants in a subclass to change default…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
8
votes
2 answers

Thread safe increment of static local variable

void foo() { static int id = 0; const int local_id = id++; //do something with local_id; } Multiple threads can call foo in parallel multiple times. I want each call of foo use "unique" value of local_id. Is it ok with the above code? I…
strugi
  • 169
  • 1
  • 1
  • 5
8
votes
1 answer

Use "FindResource" from other classes of the application

I have to use the method FindResource("key"). In my MainWindow class, it works. I've to use it in another class, but I can't refer to it with a new instance of the MainWindow class, because this gives me some problem (not relevant now). So, I have…
Piero Alberto
  • 3,823
  • 6
  • 56
  • 108
8
votes
3 answers

What are the default initialization values of static variables in c++ objects?

I believe that all numerical variables are initialized to zero, but what about things like static bool or static MyClass*? I have looked around the interwebs, but most results I found are for how to initialize things like ints to non-zero values,…
Stack Tracer
  • 968
  • 7
  • 26
8
votes
3 answers

Refactoring static method / static field for Testing

I have the following legacy code: public class MyLegacyClass { private static final String jndiName = "java:comp/env/jdbc/LegacyDataSource" public static SomeLegacyClass doSomeLegacyStuff(SomeOtherLegacyClass legacyObj) { // do…
jbandi
  • 17,499
  • 9
  • 69
  • 81
8
votes
2 answers

Can functions be optimized away if they have side effects?

I want to initialize some static data on the main thread. int32_t GetFoo(ptime t) { static HugeBarData data; return data.Baz(t); } int main() { GetFoo(); // Avoid data race on static field. // But will it be optimized away…
Sam
  • 19,708
  • 4
  • 59
  • 82
8
votes
5 answers

Call a non-static class with a console application

I'm trying to call a method from another class with a console application. The class I try to call isn't static. class Program { static void Main(string[] args) { Program p = new Program(); var myString =…
Freddy
  • 960
  • 1
  • 20
  • 46
8
votes
1 answer

Inline vs static inline in header file

To place an inline function definition in a C header file for a function that should be inlined into multiple other units, should inline or static inline be used? I've been Googling for a little while but there seems to be no concise explanation of…
Toby
  • 9,696
  • 16
  • 68
  • 132
8
votes
3 answers

Why if static method don't involve in polymorphism(late binding) I see error that static method cannot be overridden

please consider following code: class A{ public static void m(Number n){ System.out.println("Number A"); }; } class B extends A{ public static int m(Number n){ System.out.println("Number B"); return 1; …
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
8
votes
2 answers

getClass().getResource() in static context

I'm trying to get a resource (image.png, in the same package as this code) from a static method using this code: import java.net.*; public class StaticResource { public static void main(String[] args) { URL u =…
Luke Moll
  • 428
  • 6
  • 18
8
votes
4 answers

How to get a static reference to a WPF Window?

I've tried a huge amount of ways to get a static reference of my window across my program. I need to access all of its members at runtime from different classes, so a static reference is needed. What I'd like to have is something like…
Lazlo
  • 8,518
  • 14
  • 77
  • 116