What is the difference between static and non-static data members? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the difference between static and non-static data members?

3rd Feb 2018, 5:08 AM
Vansh Chopra
Vansh Chopra - avatar
4 Answers
+ 8
A non-static member is unique for each object and a static member is not. For example, if I were to define a class named “Cat” with a static member, “numOfCats” and a non-static member, “height”: Cat c1 = new Cat(); Cat c2 = new Cat(); c1.height = 5; c2.height = 7; //they each have their own height. Cat.numOfCats = 1; Cat.numOfCats++; //numOfCats is shared by all cats, and is accessed through the class name instead of the object name.
3rd Feb 2018, 5:14 AM
Jacob Pembleton
Jacob Pembleton - avatar
3rd Feb 2018, 8:38 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
@VIJAY JAGADHANE You’re thinking of final keyword.
3rd Feb 2018, 3:53 PM
Jacob Pembleton
Jacob Pembleton - avatar
- 2
static is fix value and nan static change value
3rd Feb 2018, 3:49 PM
VIJAY JAGADHANE
VIJAY JAGADHANE - avatar