0
what is static variable? why we use it?
5 Réponses
0
but sometimes we will declare some variables as static right.what is the difference between static variable and normal variables
0
Basic difference is
static members are one per class but non-static members are one per instance.
static members are accessed by their class name which encapsulates them, but non-static members are accessed by object reference.
static members can't use non-static methods without instantiating an objet, but non-static members can use static members directly.
static constructor is used to initialize static fields, but for non-static fields normal instance constructor is used
0
OK thanks
0
Welcome
- 1
Hi static variable are used in static class only. So advantage of this is if u use static class or static method u can't not access it with object. You need to call it direct so thought out the program that variable get one value example if I have non static class fruit and non static method apple so I can create object of fruit as fruit nitin =new nitin(); then will use method apple by nitin.apple() also I can create one more object like fruit sharma =new sharma() ; and use apple sharma.apple() so now that will have two different objects and values but if my class is static I have to use like fruit.apple().