Static vs Instance variables
When you declare a variable static, there is only one copy of that variable per class. Consider the following example (bad encapsulation code do not copy):
when we use static, there is only one copy per class, so no matter how many Humans objects we have, there is only one PLANET, notice we only changed planet for human 1 but the changes reflected to human 2 when we printed out the h2.PLANET.
In contrast to Instance variables, they are unique per instance of the class, h1 has a different home compared h2.
- A static variable is initialized when the JVM loads the class
static methodcannot access
Non-staticvariable or method