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
524
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,666
  • 3
  • 17
  • 16
497
votes
22 answers

Mocking static methods with Mockito

I've written a factory to produce java.sql.Connection objects: public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory { @Override public Connection getConnection() { try { return…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
479
votes
8 answers

Static vs class functions/variables in Swift classes?

The following code compiles in Swift 1.2: class myClass { static func myMethod1() { } class func myMethod2() { } static var myVar1 = "" } func doSomething() { myClass.myMethod1() myClass.myMethod2() myClass.myVar1 =…
Senseful
  • 86,719
  • 67
  • 308
  • 465
468
votes
22 answers

What does the 'static' keyword do in a class?

To be specific, I was trying this code: package hello; public class Hello { Clock clock = new Clock(); public static void main(String args[]) { clock.sayTime(); } } But it gave the error Cannot access non-static field in…
Ali
  • 261,656
  • 265
  • 575
  • 769
433
votes
9 answers

What is the difference between static func and class func in Swift?

I can see these definitions in the Swift library: extension Bool : BooleanLiteralConvertible { static func convertFromBooleanLiteral(value: Bool) -> Bool } protocol BooleanLiteralConvertible { typealias BooleanLiteralType class func…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
417
votes
5 answers

What is the lifetime of a static variable in a C++ function?

If a variable is declared as static in a function's scope it is only initialized once and retains its value between function calls. What exactly is its lifetime? When do its constructor and destructor get called? void foo() { static string…
Motti
  • 110,860
  • 49
  • 189
  • 262
382
votes
8 answers

Java: Static vs inner class

What is the difference between static and non-static nested class?
Abhishek Sanghvi
  • 4,582
  • 6
  • 28
  • 36
379
votes
9 answers

What is the difference between a static and a non-static initialization code block

My question is about one particular usage of static keyword. It is possible to use static keyword to cover a code block within a class which does not belong to any function. For example following code compiles: public class Test { private static…
Szere Dyeri
  • 14,916
  • 11
  • 39
  • 42
370
votes
13 answers

What is the reason behind "non-static method cannot be referenced from a static context"?

The very common beginner mistake is when you try to use a class property "statically" without making an instance of that class. It leaves you with the mentioned error message: You can either make the non static method static or make an instance of…
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
344
votes
7 answers

Static Block in Java

I was looking over some code the other day and I came across: static { ... } Coming from C++, I had no idea why that was there. Its not an error because the code compiled fine. What is this "static" block of code?
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
334
votes
22 answers

private final static attribute vs private final attribute

In Java, what's the difference between: private final static int NUMBER = 10; and private final int NUMBER = 10; Both are private and final, the difference is the static attribute. What's better? And why?
okami
312
votes
15 answers

How do you create a static class in C++?

How do you create a static class in C++? I should be able to do something like: cout << "bit 5 is " << BitParser::getBitAt(buffer, 5) << endl; Assuming I created the BitParser class. What would the BitParser class definition look like?
andrewrk
  • 30,272
  • 27
  • 92
  • 113
308
votes
14 answers

Static Initialization Blocks

As far as I understood the "static initialization block" is used to set values of static field if it cannot be done in one line. But I do not understand why we need a special block for that. For example we declare a field as static (without a value…
Roman
  • 124,451
  • 167
  • 349
  • 456
298
votes
9 answers

The static keyword and its various uses in C++

The keyword static is one which has several meanings in C++ that I find very confusing and I can never bend my mind around how its actually supposed to work. From what I understand there is static storage duration, which means that it lasts for the…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
297
votes
7 answers

Why doesn't Mockito mock static methods?

I read a few threads here about static methods, and I think I understand the problems misuse/excessive use of static methods can cause. But I didn't really get to the bottom of why it is hard to mock static methods. I know other mocking frameworks,…
Abidi
  • 7,846
  • 14
  • 43
  • 65