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
2514
votes
27 answers

Class (static) variables and methods

How do I create class (i.e. static) variables or methods in Python?
Andrew Walker
  • 40,984
  • 8
  • 62
  • 84
2036
votes
42 answers

Difference between static class and singleton pattern?

What real (i.e. practical) difference exists between a static class and a singleton pattern? Both can be invoked without instantiation, both provide only one "Instance" and neither of them is thread-safe. Is there any other difference?
Jorge Córdoba
  • 51,063
  • 11
  • 80
  • 130
1387
votes
21 answers

What does "static" mean in C?

I've seen the word static used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?
David
1055
votes
29 answers

What is the equivalent of Java static methods in Kotlin?

There is no static keyword in Kotlin. What is the best way to represent a static Java method in Kotlin?
pdeva
  • 43,605
  • 46
  • 133
  • 171
813
votes
43 answers

Static variables in JavaScript

How can I create static variables in Javascript?
Rajat
  • 32,970
  • 17
  • 67
  • 87
805
votes
30 answers

What is the Python equivalent of static variables inside a function?

What is the idiomatic Python equivalent of this C/C++ code? void foo() { static int counter = 0; counter++; printf("counter is %d\n", counter); } specifically, how does one implement the static member at the function level, as opposed…
andrewdotnich
  • 16,195
  • 7
  • 38
  • 57
786
votes
18 answers

Adding a favicon to a static HTML page

I have a few static pages that are just pure HTML, that we display when the server goes down. How can I put a favicon that I made (it's 16x16px and it's sitting in the same directory as the HTML file; it's called favicon.ico) as the "tab" icon as it…
legendary_rob
  • 12,792
  • 11
  • 56
  • 102
715
votes
31 answers

Why are static variables considered evil?

I am a Java programmer who is new to the corporate world. Recently I've developed an application using Groovy and Java. All through the code I wrote used quite a good number of statics. I was asked by the senior technical lot to cut down on the…
Vamsi Emani
  • 10,072
  • 9
  • 44
  • 71
672
votes
11 answers

When to use static classes in C#

Here's what MSDN has to say under When to Use Static Classes: static class CompanyInfo { public static string GetCompanyName() { return "CompanyName"; } public static string GetCompanyAddress() { return "CompanyAddress"; } //... } Use…
pbh101
  • 10,203
  • 9
  • 32
  • 31
654
votes
11 answers

What is a "static" function in C?

The question was about plain c functions, not c++ static methods, as clarified in comments. I understand what a static variable is, but what is a static function? And why is it that if I declare a function, let's say void print_matrix, in let's say…
Slava V
  • 16,686
  • 14
  • 60
  • 63
630
votes
18 answers

Can I add extension methods to an existing static class?

I'm a fan of extension methods in C#, but haven't had any success adding an extension method to a static class, such as Console. For example, if I want to add an extension to Console, called 'WriteBlueLine', so that I can…
Leon Bambrick
  • 26,009
  • 9
  • 51
  • 75
585
votes
3 answers

New self vs. new static

I am converting a PHP 5.3 library to work on PHP 5.2. The main thing standing in my way is the use of late static binding like return new static($options); , if I convert this to return new self($options) will I get the same results? What is the…
Mike
  • 12,359
  • 17
  • 65
  • 86
581
votes
22 answers

Why doesn't Java allow overriding of static methods?

Why is it not possible to override static methods? If possible, please use an example.
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
564
votes
14 answers

Change private static final field using Java reflection

I have a class with a private static final field that, unfortunately, I need to change it at run-time. Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field Is there any way to change the…
fixitagain
  • 6,543
  • 3
  • 20
  • 24
539
votes
37 answers

Why is the Java main method static?

The method signature of a Java mainmethod is: public static void main(String[] args) { ... } Is there a reason why this method must be static?
Alotor
  • 7,407
  • 12
  • 38
  • 36
1
2 3
99 100