Questions tagged [static-variables]

In object oriented programming, a static variable is a variable which belongs to the class and not to object(instance) & a single copy to be shared by all instances of the class.

In object oriented programming, a static variable is a variable which belongs to a class rather than to a particular instance of the class.

799 questions
23
votes
3 answers

Use of static variables and functions in global scope

Is there a use for flagging a variable as static, when it lies in the global scope of a .cpp file, not in a function? Can you use the static keyword for functions as well? If yes, what is their use?
org 100h
  • 233
  • 1
  • 2
  • 4
23
votes
6 answers

Static vs Instance Variables: Difference?

What is the difference between a static and instance variable. The following sentence is what I cant get: In certain cases, only one copy of a particular variable should be shared by all objects of a class- here a static variable is used. A…
user2971033
22
votes
3 answers

Why does GCC not warn for unreachable code?

Why doesn't GCC (4.6.3) give me any warning for the unreachable code in the below example? #include int status(void) { static int first_time = 1; if (first_time) { return 1; first_time = 0; /* Never reached */ …
Andreas Haas
  • 221
  • 2
  • 4
22
votes
2 answers

Local variables set to nil? (Objective-C)

I'm reading a book on Objective-C and the author said that if local variables aren't assigned a value they will be set to nil, but static variables will be set to zero. So, I set up int a and didn't assign it a value. Then NSLog(@"%i", a) to display…
stumped
  • 3,235
  • 7
  • 43
  • 76
21
votes
3 answers

Google App Engine: Memcache or Static variable?

Well, I think I have a very basic doubt here: I'm developing an app on GAE (Java) and performing a query to the datastore that returns a lot of entities, so I need to cache it. I was using memcache and it was working great, but if I keep the list…
21
votes
3 answers

Static Variables in R

I have a function in R that I call multiple times. I want to keep track of the number of times that I've called it and use that to make decisions on what to do inside of the function. Here's what I have right now: f = function( x ) { count <<-…
James Thompson
  • 46,512
  • 18
  • 65
  • 82
20
votes
2 answers

Efficient memoization in Python

I have some task to solve and the most important part at the moment is to make the script as time-efficient as possible. One of the elements I am trying to optimize is memoization within one of the functions. So my question is: Which of the…
Tadeck
  • 132,510
  • 28
  • 152
  • 198
19
votes
2 answers

Why retain a static variable?

Isn't it unnecessary to retain a static variable since it stays around for the duration of the program, no matter if you release it? See this…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
19
votes
3 answers

Reset class static variable during unit test

I am trying to write a unit test for a legacy code. The class which I'm testing has several static variables. My test case class has a few @Test methods. Hence all of them share the same state. Is there way to reset all static variables between…
kan
  • 28,279
  • 7
  • 71
  • 101
18
votes
5 answers

Static variable in asp.net page

I am having one doubt regarding the use of static variable in Asp.net pages. I am having one page say UserDetails.aspx. In this page, I have one static variable to store some data specific to a user. So, will this variable be shared across multiple…
Ashwani K
  • 7,880
  • 19
  • 63
  • 102
18
votes
3 answers

Static Function Variables and Concatenation in PHP

Consider the following: $var = 'foo' . 'bar'; # Not a member of a class, free-standing or in a function. As soon as I mark $var as static, however: static $var = 'foo' . 'bar'; PHP (5.3.1 on a WAMP setup) complains with the following error: Parse…
jklanders
  • 183
  • 1
  • 4
18
votes
5 answers

Is a static member variable common for all C# generic instantiations?

In C# I have a generic class: public class MyGeneric where ParameterClass: MyGenericParameterClass, new() { public static int Variable; } Now in C++ if I instantiated a templated class with different parameters each complete…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
17
votes
2 answers

How does local() differ from other approaches to closure in R?

Yesterday I learned from Bill Venables how local() can help create static functions and variables, e.g., example <- local({ hidden.x <- "You can't see me!" hidden.fn <- function(){ cat("\"hidden.fn()\"") } function(){ cat("You can…
David Lovell
  • 852
  • 6
  • 17
17
votes
4 answers

Why does a static variable initialized by a method call that returns another static variable remains null?

I simply don't understand the following code's execution flow: class Test { static String s1 = getVal(); static String s2 = "S2"; private static String getVal() { return s2; } public static void main(String args[]) { …
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
17
votes
5 answers

Do I get improved performance by making variables static?

Why do some people declare make their variables static, like so: char baa(int x) { static char foo[] = " .. "; return foo[x ..]; } instead of: char baa(int x) { char foo[] = " .. "; return foo[x ..]; } It seems to be very common on…
Jack
  • 16,276
  • 55
  • 159
  • 284
1
2
3
53 54