Questions tagged [static-members]

A static member is a data field that is shared by all instances of a class or structure for the same program. Static member exists even when no objects of the static data member's class exist. A feature of C++, roughly equivalent to Java static fields.

Static member can be accessed using type qualifier, even if there is no instance of the class. They must be declared outside the class declaration:

// class declaration:
class a_class  { static int sf;  };

// field declaration:
int a_class::sf = 17;

Apart data fields, it can be static methods that have no access to the non-static fields but for that can be invoked without having the instance, also by type qualifier:

struct a_struct { static long the_function(); };
...
long v = a_struct::the_function();
1180 questions
625
votes
18 answers

How to initialize private static members in C++?

What is the best way to initialize a private, static data member in C++? I tried this in my header file, but it gives me weird linker errors: class foo { private: static int i; }; int foo::i = 0; I'm guessing this is because I can't…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
234
votes
6 answers

Undefined reference to static constexpr char[]

I want to have a static const char array in my class. GCC complained and told me I should use constexpr, although now it's telling me it's an undefined reference. If I make the array a non-member then it compiles. What is going on? // .hpp struct…
Pubby
  • 51,882
  • 13
  • 139
  • 180
219
votes
10 answers

How to initialize static variables

I have this code: private static $dates = array( 'start' => mktime( 0, 0, 0, 7, 30, 2009), // Start date 'end' => mktime( 0, 0, 0, 8, 2, 2009), // End date 'close' => mktime(23, 59, 59, 7, 20, 2009), // Date when registration…
Svish
  • 152,914
  • 173
  • 462
  • 620
190
votes
8 answers

PHP 5: const vs static

In PHP 5, what is the difference between using const and static? When is each appropriate? And what role does public, protected and private play - if any?
Chris Jacob
  • 11,878
  • 7
  • 47
  • 42
169
votes
1 answer

Static member functions error; How to properly write the signature?

I am getting an error when trying to compile my code in g++ using the current signature: cannot declare member function static void Foo::Bar(std::ostream&, const Foo::Node*) to have static linkage My question is twofold: Why does it not Compile…
Joshua
  • 4,270
  • 10
  • 42
  • 62
122
votes
7 answers

Error message Strict standards: Non-static method should not be called statically in php

I have the following php. However when I see the index.php I get the following error message. Strict standards: Non-static method Page::getInstanceByName() should not be called statically in /var/www/webworks/index.php on line 12 I am…
shin
  • 31,901
  • 69
  • 184
  • 271
107
votes
4 answers

Android static object lifecycle

I am creating event search application, we set search criteria from one screen populate in another screen then user can edit search criteria from 3rd screen and goes to 4th screen. To achieve above task i am using static object which remember the…
d-man
  • 57,473
  • 85
  • 212
  • 296
104
votes
8 answers

Which is best way to define constants in android, either static class, interface or xml resource?

I'm developing an android application which uses web service to get data from server, for that I'm having three different set of URLs to point development system, test server and live server. It's difficult to change URL whenever I want to give…
Jayabal
  • 3,619
  • 3
  • 24
  • 32
102
votes
6 answers

Are static fields open for garbage collection?

Given an hypothetical utility class that is used only in program setup: class MyUtils { private static MyObject myObject = new MyObject(); /*package*/static boolean doStuff(Params... params) { // do stuff with myObject and params... …
Michael Deardeuff
  • 10,386
  • 5
  • 51
  • 74
94
votes
4 answers

Static enum vs. Non-static enum

What's the difference between static and non-static enum in Java? Both usages are same. Is it correct that all static ones are loaded on memory on startup, and non-static ones are loaded on demand? If yes, then which method is better? Keeping some…
AHHP
  • 2,967
  • 3
  • 33
  • 41
91
votes
12 answers

Why does Java prohibit static fields in inner classes?

class OuterClass { class InnerClass { static int i = 100; // compile error static void f() { } // compile error } } Although it's not possible to access the static field with OuterClass.InnerClass.i, if I want to record something that should…
Jichao
  • 40,341
  • 47
  • 125
  • 198
74
votes
1 answer

Implicitly lazy static members in Swift

I just noticed that static members of Swift structs are implicitly lazy. For instance, this will only call the init once: class Baz { init(){ print("initializing a Baz") } } struct Foo { static let bar = Baz() } var z =…
cfischer
  • 24,452
  • 37
  • 131
  • 214
66
votes
11 answers

How exactly do static fields work internally?

Say you have a class, class Foo { public static bar; } When you say: new Foo(); I can imagine that in memory, a space is reserved for this object. ...and when you say again: new Foo(); ...well now you have another space available for the…
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
64
votes
3 answers

How to have static data members in a header-only library?

What is the best way to have a static member in a non-templated library class, without placing the burden of defining the member on the class user? Say I want to provide this class: class i_want_a_static_member { static expensive_resource…
pesche
  • 3,054
  • 4
  • 34
  • 35
63
votes
4 answers

Mock private static final field using mockito or Jmockit

I am using private static final LOGGER field in my class and I want LOGGER.isInfoEnabled() method to return false. How can I mock the static final field by using mockito or jMockit My class is: import org.slf4j.Logger; import…
RaT
  • 1,134
  • 3
  • 12
  • 28
1
2 3
78 79