Probably; I don't see anything in the standard that says you can't. However, the use of the volatile keyword is usually in embedded code, or multi-threading in which you don't want the compiler to do optimizations on the variable itself.
Yes, sure.
Chat with our AI personalities
No. A constant is a value that does not ever change. A variable is a value that can be changed but only from within the program. A volatile is value that may be changed from outwith the program.
In mathematics, a constant is a known which does not change, i.e. it stays constant. A variable is an unknown which can change depending on circumstances.
StaticIn the C language family, a static variable is one that exists for the lifetime of a compilation unit (a source file or module). A static variable can be declared module-wide, and thus be accessed by all functions defined within the same source file. Such a static variable cannot be directly accessed from other modules, but inner-module API can pass pointers to static variables and modify those through pointers.A static variable can also be declared within a function body, where the usual scope rules apply. A static variable declared within a function is only initialized when the module is initialized (typically when the application loads), and preserves its values over multiple invocations of the function that contains the definition.In C++, a static variable can also be a member of a class definition. Access to a static member variable is governed by the standard access modifiers (private, public, protected), but all instances of this class share the same static variable, and share the same value. Modifying the value of this variable affects all objects of the class.VolatileThe volatile keyword is something all together different, and not in any way an opposite to static. A static variable may or may not be declared volatile, just as a global or local variable can be.The volatile keyword is a hint informing the compiler that the variable's value might change without the compiler's knowledge. Therefore, the compiler's code optimizer cannot make assumptions about the variable's current value, and must always (re-) read the variable's content.Volatile variables are sometimes used in context with interrupt handlers (although those normally benefit from more sophisticated synchronization methods such as semaphores, mutexes or locks). Most typically, volatile variables are used to model hardware registers through variables.For example, consider a hardware register at address 0x1234, containing a single byte. The value of this byte is hardware driven and contains, for example, the current ambient light level in a range of 0..255. Such a register could be modeled with this construct:unsigned char *const volatile lux = (unsigned char *)0x1234;unsigned char firstReading = *lux;unsigned char secondReading = *lux;In this example, the volatile keyword ensures that the second reading is taken from the hardware register again. Without, a good compiler will read the memory address once and assign the same value to both variables.
For C programming, the use of a static variable has two uses: One reason is to hide the variable from other modules. The scope of the static variable is limited to the compilation unit that it is described in. The second use of a static variable is to keep the value of the variable intact through the entire program execution unit.
difference between constant and static variables in java
A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.
It is, provided that the variable is also static or you're referencing the variable through an instance of you class. class MyClass { static int staticVariable; int memberVariable; public static void main(String[] args) { // This is legal. staticVariable = 10; // This is not legal. memberVariable = 10; // This is legal. MyClass mc = new MyClass(); mc.memberVariable = 10; } }