answersLogoWhite

0


Best Answer
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.

Volatile

The 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.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

They are entirely different things, there is no point to comparing them.

constant: it's value is not supposed to be changed.

volatile: it's value can be changed asynchronously (by an interrupt, for example).

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A const data type is one which cannot be changed once initialized.

A volatile data type is one which should never have its value locally cached.

Note: Neither of them is data-type, they are modifiers.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Instance variables only apply to objects of a class (instances of the class). Every object has its own independent set of instance variables but there can only ever be one instance of a static variable. Static variables are instantiated and initialized at program load time and exist throughout the life-cycle of the program regardless of where they are declared. All global variables and constants are also implicitly static. Global variables need not be initialized at load time but they must be initialized at run-time before first use.

Since static variables exist at all times, they can maintain their value across scopes. A (non-static) variable declared inside a function is instantiated when the function is invoked and destroyed when the function falls from scope, whereas a static variable exists whether the function is in scope or not, but is only accessible while the function is in scope. This is useful when an otherwise global variable is only required by one specific function.

Static variables of a class (static members) are local to the class rather than local to objects of the class. As a member of its class, any object of the class may alter the value of a static member variable. There being only one instance of the variable, this provides a means of sharing a value amongst all objects of that class. When declared public, a static member variable is also accessible outside of the class even when no objects of that class exist. Public static member variables are often used in preference to global variables because they are localized to the class, but if the variable holds an invariant it is better to declare the variable private and provide public interface to encapsulate the invariant.

Static variables can also be explicitly declared in the global namespace (at file scope), but this is generally frowned upon unless the variable expresses the notion of a truly global concept. Localizing variables provides better encapsulation and maintainability, limiting accessibility to only the code that actually requires accessibility, whether at function scope, class scope or package scope.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The using of the volatile keyword.

example:

int n1;

volatile int n2;

Modifier volatile is a hint to the compiler to not register-optimize the variable, because its value can be changed asynchronously (i.e., in another thread or process). Example:

static volatile int interrupted= 0;

static void signal_handler (int signo)

{

interrupted= 1;

}

int main (void)

{

signal (SIGINT, signal_handler);

while (!interrupted) ...

}

it wouldn't work if the compiler assumed that the value of interrupted can be stored in a register (or simply assumed to be constant).

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

static variable also retains its value , at each post back,therby no need to use state management techniques if one is using static variables.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Yes.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between static methods and static variables?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

WAP to show the difference between a variable and static variable?

difference between constant and static variables in java


It is perfectly legal to any instance variable inside of a static method?

No. You will get compilation errors. The complier will complain that you are trying to access non static variables from inside a static method. A static method can access only static variables.


Can a static method access instance variables?

Variables cannot access variables; only methods can access variables. Non-static methods (also known as instance methods) are local to an object of the class and therefore have access to a "this" reference (referring to the current instance of the class, the object upon which the method was invoked), but static variables are local to the class itself. These variables are shared by all objects of the class and are therefore accessible to non-static methods. Static variable are also accessible to static methods and are therefore accessible even when no objects of the class exist.


What is the difference between a class method and an instance method?

Instance methods can be called by the object of a Class whereas static method are called by the Class. When objects of a Class are created, they have their own copy of instance methods and variables, stored in different memory locations. Static Methods and variables are shared among all the objects of the Class, stored in one fixed location in memory.Static methods cannotaccess instance variables or instance methods directly-they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.


What kinds of members can a class have?

Members of a class may include 1. Static Methods 2. non-static methods 3. Instance variables 4. Sub classes etc.

Related questions

WAP to show the difference between a variable and static variable?

difference between constant and static variables in java


How do you access the static variable and static methods of a class?

In java we access static variables and static methods without creating objects. i.e.,we can access directly by using classname we can also access static variables and static methods by using objects which are created by using class where the static variables and static methods are available


Can non static methods can access static variables?

Yes, they can


It is perfectly legal to any instance variable inside of a static method?

No. You will get compilation errors. The complier will complain that you are trying to access non static variables from inside a static method. A static method can access only static variables.


What is the difference between continuous and discrete system?

The difference between continuous and discrete system lies in the variables. Whereas the continuous systems have dynamic variables, the discrete system have static variables.


Can a static method access instance variables?

Variables cannot access variables; only methods can access variables. Non-static methods (also known as instance methods) are local to an object of the class and therefore have access to a "this" reference (referring to the current instance of the class, the object upon which the method was invoked), but static variables are local to the class itself. These variables are shared by all objects of the class and are therefore accessible to non-static methods. Static variable are also accessible to static methods and are therefore accessible even when no objects of the class exist.


What is the difference between a class method and an instance method?

Instance methods can be called by the object of a Class whereas static method are called by the Class. When objects of a Class are created, they have their own copy of instance methods and variables, stored in different memory locations. Static Methods and variables are shared among all the objects of the Class, stored in one fixed location in memory.Static methods cannotaccess instance variables or instance methods directly-they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.


When should you use static method and when should we use instance method?

Non-static methods are the "normal" type of methods for a class to have. They use instance variables to perform certain actions. In other words, object A and object B of the same class may behave differently when we call one of their Non-static methods depending on the value of their instance variables. Static methods on the other hand behave the exact same way for all instances of a class. object A and B of the same class will act in the same way when we call one of their Static methods. (*NOTE* Static methods cannot use instance variables)


What is Static method in programming?

A static method in java is also named a class method, because it does not need an instance (of his class) to be invoked. Static methods can't use instance variables (non static variables) or use the keywords 'this'. These methods receive all the information they need to complete his task from his parameters


What is the difference between static and dynamic variables?

Static variables (should) remain the same e.g. temperature of a water bath, k constant of a particular spring. Dynamic variables change as the experiment progresses e.g. air temperature and pressure, amount of natural light.


What kinds of members can a class have?

Members of a class may include 1. Static Methods 2. non-static methods 3. Instance variables 4. Sub classes etc.


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.