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.

15337 questions
2140
votes
23 answers

Are static class variables possible in Python?

Is it possible to have static class variables or methods in Python? What syntax is required to do this?
Andrew Walker
  • 36,318
  • 7
  • 54
  • 80
1872
votes
40 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
  • 47,433
  • 11
  • 77
  • 126
1245
votes
19 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
762
votes
42 answers

Static variables in JavaScript

How can I create static variables in Javascript?
Rajat
  • 29,134
  • 17
  • 60
  • 84
761
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
  • 36,445
  • 42
  • 122
  • 154
723
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…
TheLegend
  • 11,612
  • 10
  • 54
  • 89
661
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
  • 9,352
  • 7
  • 39
  • 69
644
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
  • 9,713
  • 8
  • 29
  • 31
566
votes
12 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
  • 14,804
  • 13
  • 53
  • 60
564
votes
15 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
  • 24,569
  • 9
  • 48
  • 74
554
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.
sgokhales
  • 49,762
  • 33
  • 123
  • 158
536
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
  • 11,085
  • 15
  • 55
  • 82
517
votes
37 answers

Why is the Java main method static?

The method signature of a Java main() method is: public static void main(String[] args){ ... } Is there a reason for this method to be static?
Alotor
  • 7,117
  • 12
  • 34
  • 36
506
votes
12 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
  • 5,953
  • 3
  • 17
  • 24
474
votes
11 answers

Static constant string (class member)

I'd like to have a private static constant for a class (in this case a shape-factory). I'd like to have something of the sort. class A { private: static const string RECTANGLE = "rectangle"; } Unfortunately I get all sorts of error from…
lb.
  • 5,160
  • 2
  • 14
  • 16
1
2 3
99 100