What is the difference "static int" and simple "int"? | SoloLearn: Learn to code for FREE!

+11

What is the difference "static int" and simple "int"?

static int X = 10; int B=3;

9/10/2017 4:36:05 PM

Anton

23 Answers

New Answer

+10

😃 it's static because it does bot move from object to object, it stays with the class 😃

+19

Agree, but would prefer 'static variables are shared among all objects of a class' rather than 'only have one instance'.

+12

static makes the member bound to the class. Or in other words, only have one instance.

+11

When I think about 'static', I imagine things bound to the class *definition*. There can only be ONE definition, while there can be many instantiations (objects created from the definition). Like an engineering blueprint... you can build many buildings, but you don't write engineering notes / hints on the buildings; you write it on the one blueprint. Since this definition is technically 'outside', anyone in a constructed building can go back and see the single, static (fixed) copy of your notes on the building plan.

+9

When you use static keyword on a property or method, that means that that property or method belongs to the class itself and not the objects you are creating based on that class. So, all objects will share this property. That means that all of them will see the same value. Objects have a state. For example, customer 1 is called Jack, customer 2 is called John. So the state in this case is the values they hold like name="Jack". This belongs to customer 1 and no other one shares it. But if you use the keyword static in your property, then all objects you create will see the same name, Jack. Now, calling every customer Jack is not wise. So, each object has its own state. In other words, each customer object has its own name, like: John, Jack, Bob, etc. However, there are moments where you would want to share data across objects created from the same class and that you can do with keyword 'static'.

+7

It is object independent. the static keyword also allows you to use a class member without creating an object. So "static int" has the potential to be accessed without an object while "int" does not.

+4

Thanks guys

+4

static int means...every object of that class will have one copy of the static variable...i.e any change done using one object will also reflect in other...but whereas simple int ... every object will have its own copy and changes done to it will not reflect on others objects☺️

+3

static has to with the stack , alloc mem static or dynamanic

+3

https://code.sololearn.com/cBznw3b7bhi8/?ref=app

+3

This mite help you out to see what C/C++ does Helped me, dont run good on phone try a pc or laptop https://godbolt.org/

+3

static int will be class level variable, can be accessed within a class and its value can not be changed... howevt int b can be global local variable and its value can be changed... really simple

+2

static int value will never be garbage.. for class static is not for any objects static int is property of class not of object like int is property of any object.

+1

static int preserved it's value throughtout the program that is not in simple int

+1

https://code.sololearn.com/cBznw3b7bhi8/?ref=app Thy bro:)

+1

ok, thanks for the compiler.

+1

🙃

+1

static int has not one thing to do with a class or its object static came from C programing none objective programing object means Myclass m; m.whatever vec.at() etc.....

+1

Static int retain their values when they are being called more than onces why int doesn't.

0