Static members
A class can be declared static only when it is a nested class. Class members—methods and fields—can be static too, as long as the class is not anonymous or local. Any code can access a static member of a class without creating a class instance (object). We saw such an example in previous sections when we used the class Math in one of the code snippets. A static class member is also called a class variable in case of the field or a class method in case of a method. Notice that these names contain the word class as an adjective. That is because a static member is associated with a class, not a class instance. It means that there is only one copy of a static member that can exist in JVM memory, although many instances (objects) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access