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
6 answers

Why are static classes used?

I have doubts on static class and static methods. From MSDN I understood that "Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class." So if we don't want to associate a…
8
votes
11 answers

static vs non-static method for immutable class

Given the class definition below. How would one go about deciding whether the stub methods should be static or non-static? class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; …
user63904
8
votes
4 answers

Decimal.MinValue & Decimal.MaxValue: why static readonly and not const modifiers?

In C#, MinValue field is defined for numeric types with: ① static readonly modifiers for decimal type (Link to MSDN Libray for .NET 4.5): public static readonly decimal MinValue ② const modifier for all other numeric types: //Integral signed…
Fabien Launay
  • 639
  • 7
  • 16
8
votes
5 answers

C++: Undefined reference to instance in Singleton class

I'm currently trying to implement a factory as a singleton. I practically used the textbook example of the Singleton pattern. Here's the .h file: namespace oxygen{ class ImpFactory{ public: static boost::shared_ptr
aheld
  • 313
  • 1
  • 5
  • 11
8
votes
2 answers

Is it possible to execute static code using Dart?

Both Java and Javascript allow for a different way of executing static code. Java allows you to have static code in the body of a class while JS allows you to execute static code outside class definitions. Examples: Java: public class MyClass { …
Steven Roose
  • 2,731
  • 4
  • 29
  • 46
8
votes
1 answer

FindBugs error: Write to static field from instance method

I have couple of areas in my application where I get the error while manipulating value of static variable from instance method. "Write to static field from instance method". If we take multi-threading out of the equation, does this scenario pose…
Baz
  • 113
  • 1
  • 1
  • 8
8
votes
2 answers

in Dart, problems with static method when called from variable

have class Klass with static method fn1 class Klass { static String fn1() => 'hello'; } > Klass.fn1(); // hello but when Klass is assigned to a variable, calling the method fn1 fails var k = Klass; > k.fn1() // "Unhandled exception: Class…
cc young
  • 18,939
  • 31
  • 90
  • 148
8
votes
3 answers

Why doesn't calling a static variable chained from a static method that returns null throw a NPE?

I have the following code public class Test { static String mountain = "Everest"; static Test favorite() { System.out.print("Mount "); return null; } public static void main(String[] args) { …
MaheshVarma
  • 2,081
  • 7
  • 35
  • 58
8
votes
2 answers

Why toString() cannot be a static method?

Sup guys, I have a simple, but bugging question. As far as I understand, static basically means, that for every single instance of that class, this method will be the same, if we change it, this will change for every single instance of that class,…
Chris Dobkowski
  • 325
  • 4
  • 13
8
votes
4 answers

What is the purpose of defining an inner class within a static method?

I was reading the book "Head First Java" and at some point it mentioned that an inner class instance must be tied to an outer class instance, which I was already aware of, but with an exception: A very special case—an inner class defined within a…
federico-t
  • 12,014
  • 19
  • 67
  • 111
8
votes
1 answer

To create a persistant variable is it better to have a local static variable or a global?

Lets say you have a class (c++) or module (single c file). Then in one of your functions you want to store a copy of a variable and hold its value until the next time the function is called, is it better to have a global (could be private in c++ and…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
8
votes
2 answers

Static Map as Class Member in C++

I'm having trouble using a static map as a C++ member. My header file is: class Article { public: // static map dictionary; .... .... }; In my constructor I call the following method first: void Article::InitializeDictionary() …
Kyle
  • 152
  • 2
  • 2
  • 7
8
votes
1 answer

How to declare an static instance of a Class in C++?

Some months ago I created a C# Project of a game called Vexed. Now I'm creating a Tetris, but in C++. Basically I want to use the same logic I used in the other project, it was a little bit like this: I created a Class called "Game" where there was…
avatarbobo
  • 269
  • 2
  • 6
  • 14
8
votes
1 answer

Static (iPhone) libraries, distribution, and dependencies

(Presumably the following question is not iPhone specific, aside from the fact that we would likely use a Framework or dynamic library otherwise.) I am building a proprietary iPhone SDK for a client, to integrate with their web back-end. Since we…
Mirko Froehlich
  • 12,006
  • 4
  • 27
  • 23
8
votes
5 answers

C- Linkage of functions declared static

Functions and variables declared static have internal linkage and they have file scope and they are not visible to functions in other files. Suppose that i declare a function like this:- static int foo(int i); in one file named file1.c Can I…
chanzerre
  • 2,409
  • 5
  • 20
  • 28
1 2 3
99
100