+ 5

What is the output of this line?can we change static variable value ?if yes ,y it is said static?why to use static keyword?

static int a=7; a=11; cout<<++a;

4th Dec 2016, 11:09 AM
sadiya zainab
sadiya zainab - avatar
6 Réponses
+ 2
Output will be 12 Yes, values of the static variables can be changed. It is said static because only 1 copy or the original copy of a static variable is shared by all objects using it, whereas for non-static variables each object have its own copy of that variable. As only a single copy of static variable is shared among the objects so if 1 objects modifies the value of the static variable then the value is modified for all objects using that static variable, this is the reason we use static variable; i.e. to maintain concurrency in data.
3rd May 2018, 8:06 PM
Yash Saxena
Yash Saxena - avatar
0
First, please give us detailed information about your problem, for example here misses the programming language. Due to your problem we can solve this even without this information. Static is a modifier for object orientated programming. It has nothing to do with access in your example. For explaining static, we first start with a class. When you write a class object and insert there a static variable "staticx", a normal variable "x", a static method "staticFunc" and a normal method "func". Now staticx and staticFunc are class owned stuf, which means you can access them without instantiating an object of the class. In opposte to that you need to instantiate an object of the class to access x and func.
4th Dec 2016, 11:41 AM
Andreas K
Andreas K - avatar
0
dude its clear that this is c++. It should be a hint to give more detailed information next time.
5th Dec 2016, 8:48 AM
Andreas K
Andreas K - avatar
0
error hoga may be
16th Dec 2016, 9:37 AM
Badal
Badal - avatar
0
ya fir 12 hona chachiye
16th Dec 2016, 9:40 AM
Badal
Badal - avatar
0
A static variable works almost same as global variable.The difference is it's scope is only limited in that file and you can declare it in a function
29th Dec 2016, 12:58 AM
Jawwad Hossain
Jawwad Hossain - avatar