Java Static Fields vs Instance Fields: Understanding the Difference

One cool thing I just understood about the static members in Java.... Instance fields live inside objects, which means each object has its own copy. But Static fields, on the otherhand live in the class, and all objects(instance(s) of the class) share one copy. From the example below, So if I create a "firstEmployee" and "secondEmployee" objects, I won't be able to access the "counter" field from those objects. But I would be able to do it directly on the Employee class. I can access the counter and check how many employees have been created, which in this case, is two. So always remember that Static members belong to the class, not the objects of the class. They ideally don't have access to individual object data. Hope this distinction makes sense? #mikeTriesJava #java #springboot #backend

  • text

To view or add a comment, sign in

Explore content categories