What Does Static Mean In Java?
December 15, 2023
Students are often baffled by strings, classes, instances, and many other terms when they start learning a new programming language. Some students asked what does static mean in Java?
Concisely, static is a keyword to create methods and fields that belong to the class, as opposed to an instance of the class. This way, static variables can be shared by all instances of a class.
To understand the concept of static in Java, it would be beneficial to know a little bit about classes and variables. Let us start with classes. The class is a concept in programming languages that defines a broad type of objects. For example, if “fruits” is a class then apple, peach, and banana can be its subclasses. To work with classes, we “declare” a class and its subclasses. A subclass inherits all the fields and methods of its class and then adds additional fields.
There are different kinds of variables. Member variables in a class are called fields, variables in a block of code or method are called local variables, and variables in method declarations are called parameters.
When you create many objects from the same class blueprint, they have their own unique copies of instance variables. In the case of fruits class, the instance variables can be weight, color, and sweetness. Static modifier is used to have variables that are common to all objects. Fields with static modifier are called class variables or static fields. They are associated with the class, and every instance of the class shares the class variable that is present in a fixed location in memory.
A good rule is to use static in the main () method of your application only. In fact, there are very few instances where you may want to use the static modifier. For example, when you are defining global variables.