Static | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Static

What is the different between static and -“non-static”? Why do we need that not making instance thing and where is better to use the other one?

8th Jun 2018, 4:21 AM
Tomáš Beránek
Tomáš Beránek - avatar
1 Answer
+ 4
A static member of a class belongs to the class and not to the instance of a class. For example, a counter to count how many objects which has been instantiated. We clearly don't want the counter to be re-instantiated to 0 with every new object. We also don't want every object to have it's own counter - We want the class to have a counter, so we declare it as static. It looks something like: class A { static int counter = 0; A () { counter++; } }; There are a handful of threads about the static keyword. You may look them up using the search bar.
8th Jun 2018, 4:25 AM
Hatsy Rei
Hatsy Rei - avatar