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

get caller class name from inherited static method

I have following classes (note that methods are static): class Base { public static void whosYourDaddy() { Class callerClass = // what should I write here to get caller class? System.out.print(callerClass.getName()); } } Class…
Greg Witczak
  • 1,634
  • 4
  • 27
  • 56
8
votes
2 answers

C: Assigning "static const char * const" to "static const char *"

I have a program with some global strings defined at the top of the file like this: static const char * const STRING_A = "STRING A"; static const char * const STRING_B = "STRING B"; Then in the main program loop I repeatedly call a function. This…
Adam Goodwin
  • 3,951
  • 5
  • 28
  • 33
8
votes
7 answers

static - used only for limiting scope?

Is the static keyword in C used only for limiting the scope of a variable to a single file? I need to know if I understood this right. Please assume the following 3 files, file1.c int a; file2.c int b; file3.c static int c; Now, if the 3 files…
chronodekar
  • 2,616
  • 6
  • 31
  • 36
8
votes
4 answers

Inner static class inside inner class cannot be converted

Inspired in this question: How to implements Iterable I decided to make a basic linked list implementation and implement an iterator in order to have a code like this: MyList myList = new…
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
8
votes
3 answers

ios - How to declare static variable?

Static Variable declared in C# like this : private const string Host = "http://80dfgf7c22634nbbfb82339d46.cloudapp.net/"; private const string ServiceEndPoint = "DownloadService.svc/"; private const string ServiceBaseUrl = Host +…
user2439531
  • 131
  • 1
  • 2
  • 7
8
votes
2 answers

Binding object instances to static closures

Is it possible to bind an instance to a static closure, or to create a non-static closure inside of a static class method? This is what I mean...
Nate Higgins
  • 2,104
  • 16
  • 21
8
votes
4 answers

How to initialize a static const set in implementation file?

My initialisation of a static const set appears to be incorrect, I'd appreciate your guidelines on this: obj.h: class obj { ... private: static const set keywords; ... } obj.cpp: const string kw[] =…
Prem
  • 3,373
  • 7
  • 29
  • 43
8
votes
13 answers

Does static imply no state

I recently made a recommendation to one of my colleagues stating that on our current project (C#) "services should be stateless and therefore static". My colleague agreed and indicated that in our project the services are (and should be) indeed…
Kane
  • 16,471
  • 11
  • 61
  • 86
8
votes
3 answers

Setting static variables in Objective C

I can't seem to find a way to set the static int I have created to assign unique ids to every object I save to persistent memory. The following gives me a 'no setter method 'setIdGen' for assignment to property. -(void)viewDidLoad { …
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
8
votes
2 answers

non-static variable this cannot be referenced from a static context

The error comes from this line BoardState addme = new BoardState(); For some reason the non-static variable that it is pointing at is "new". I am unclear of how I can fix this error as new is not meant to be a variable, and is not. Looking…
John Smith
  • 83
  • 1
  • 1
  • 3
8
votes
1 answer

Nginx Cache-Control header not working (getting 404 on logs)

I'm trying to setup nginx to cache static files, such as images, css and js. This is my conf. server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location /…
Philip
  • 6,827
  • 13
  • 75
  • 104
8
votes
5 answers

5 ways to use the static keyword in Java

I just had an interview where one of the questions was something like "Describe 5 ways to use the static keyword in Java." I could only think of 2 on the spot, and afterwards I found 2 more. What is the 5th? Declaring a field belonging to a class…
Sam
  • 2,620
  • 2
  • 26
  • 28
8
votes
4 answers

How SHOULD you make (and use) static libraries on the iPhone

AFAICS, any serious iPhone developer must make and use static libs on a regular basis, or else condemn themselves to buggy, hard-to-maintain, unwieldy projects. But Apple refuses to provide any official docs on the process (just circular references:…
Adam
  • 32,900
  • 16
  • 126
  • 153
8
votes
3 answers

What is the difference between an instance and a class (static) variable in Java

The title of this question is actually a previous examination question and I am looking for clarification / an answer to it. Please note that I am learning Java and am becoming familiar with its syntax. I understand that this question may have been…
PrimalScientist
  • 831
  • 5
  • 17
  • 27
8
votes
3 answers

Converting procedural PHP into object-oriented PHP

I currently have a fairly large application written entirely with procedural PHP. I am looking to further my PHP experience and recode a majority of my application using object-oriented techniques. There are a lot of areas where OOP can help reduce…
Steffan Long
  • 337
  • 2
  • 12
1 2 3
99
100