static-variables

NullPointerException with static variables

假如想象 提交于 2020-01-12 08:05:26
问题 I just hit very strange (to me) behaviour of java. I have following classes: public abstract class Unit { public static final Unit KM = KMUnit.INSTANCE; public static final Unit METERS = MeterUnit.INSTANCE; protected Unit() { } public abstract double getValueInUnit(double value, Unit unit); protected abstract double getValueInMeters(double value); } And: public class KMUnit extends Unit { public static final Unit INSTANCE = new KMUnit(); private KMUnit() { } //here are abstract methods

static variables in application scope are null when app crashes

a 夏天 提交于 2020-01-05 08:10:52
问题 I have some static variables in a class which extending the Application class .Now, somewhere in app ,if crash takes place then these are becoming null why it is so. can i say clearly like, I am declaring those two variables in Application sub class and initializing in other java class when crash occurs these becoming null.my app requirement is after login only those two should be initialized. For any answer/advice Thanks in advance 回答1: Static variables are associated with a class and they

Using static function variable vs class variable to store some state

烂漫一生 提交于 2020-01-03 13:36:33
问题 Lets say I have function like this: void processElement() { doSomething(someArray[lastProcessedElement + 1]); } The thing is, every time this function called, I need to the store the last element that I called doSomething on. So in here I have two choices: I can create a private class variable named lastProcessedElement and increment it's value every time that function is called. This approach seems most common. So the code can be something like this: class Foo { int lastProcessedElement = 0;

Using static function variable vs class variable to store some state

ε祈祈猫儿з 提交于 2020-01-03 13:36:06
问题 Lets say I have function like this: void processElement() { doSomething(someArray[lastProcessedElement + 1]); } The thing is, every time this function called, I need to the store the last element that I called doSomething on. So in here I have two choices: I can create a private class variable named lastProcessedElement and increment it's value every time that function is called. This approach seems most common. So the code can be something like this: class Foo { int lastProcessedElement = 0;

What is the difference between a static const and constexpr variable?

£可爱£侵袭症+ 提交于 2020-01-02 03:24:07
问题 I understand that a constexpr variable can be used at compiletime. For a template, or static asser for instance. But if I want to do that without constexpr I can with static const . What is since C++11/14 introduced constexpr the difference between constexpr int a = 3; //AND static const int a = 3; Thank you! Another way to see this question is which should I use? 回答1: The main difference that I know is, the value of constexpr must be known in compile-time while a const static can be assigned

Static variables in instance methods

大兔子大兔子 提交于 2020-01-01 04:30:07
问题 Let's say I have this program: class Foo { public: unsigned int bar () { static unsigned int counter = 0; return counter++; } }; int main () { Foo a; Foo b; } (Of course this example makes no sense since I'd obviously declare "counter" as a private attribute, but it's just to illustrate the problem). I'd like to know how C++ behaves in this kind of situation: will the variable "counter" in the bar() method be the same for every instance? 回答1: Yes, counter will be shared across all instances

What is the difference between .LIB and .OBJ files? (Visual Studio C++)

坚强是说给别人听的谎言 提交于 2019-12-29 04:30:09
问题 I know .OBJ is the result of compiling a unit of compilation and .LIB is a static library that can be created from several .OBJ, but this difference seems to be only in the number of units of compilation. Is there any other difference? Is it the same or different file format? I have come to this question when wondering if the same static variable defined in two (or more) .LIBs is merged or not during linking into the final executable. For .OBJs the variables are merged. But is it the same in

What's the meaning of static variables in an implementation of an interface?

纵然是瞬间 提交于 2019-12-28 04:45:29
问题 I don't quite understand static variables when defined in the implementation of an interface. In methods I do understand how they differ from local variables, but not when defined directly in an implementation. Look at these examples. What difference do these two make practically? #include "MyClass.h" @implementation MyClass int myInt; ... @end And: #include "MyClass.h" @implementation MyClass static int myInt; ... @end myInt is in both cases visible to all the methods, and if I interpreted a

undefined reference with a static instance

一个人想着一个人 提交于 2019-12-24 16:16:34
问题 I am currently porting some code from Visual Studio to Mingw GCC. It seems there is a problem when I attempt to use a static variable . This code works fine in Visual studio so I am not sure what the problem might be here. I have pasted an outline of what the code looks like. If this is insufficient I could put in more. Header file: .h namespace LG_Wrapper { template <LG_Thread Thread> class EffectApplication : public ktApplication { public: static EffectApplication<Thread>& GetInstance();

PHP static variables across multiple .php pages

我的未来我决定 提交于 2019-12-24 11:34:39
问题 I am building a POP3 mailbox in PHP. I have the following files : server_access.php (fetch mails from the POP3 server) data_access.php (which fetches/writes mails to local DB) mime_parser.php (parses MIME content) core.php (uses above files and stores parsed mail as an assoc array called $inbox) Now, I have the pages mailbox.php to show the inbox and showmail.php to display each mail. The user's credentials are stored in a .ini file and used as necessary. The thing is, I do a require_once(