what is static variable? why we use it? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

what is static variable? why we use it?

25th Jun 2016, 12:08 PM
Vijaya Lakshmi
Vijaya Lakshmi - avatar
5 Respostas
0
but sometimes we will declare some variables as static right.what is the difference between static variable and normal variables
25th Jun 2016, 12:46 PM
Vijaya Lakshmi
Vijaya Lakshmi - avatar
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
25th Jun 2016, 12:50 PM
nitin sharma
nitin sharma - avatar
0
OK thanks
25th Jun 2016, 12:54 PM
Vijaya Lakshmi
Vijaya Lakshmi - avatar
0
Welcome
25th Jun 2016, 12:58 PM
nitin sharma
nitin sharma - avatar
- 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().
25th Jun 2016, 12:44 PM
nitin sharma
nitin sharma - avatar