In Java
, the static
keyword can be applied to a nested class
, method
, variable
, and a block
. Essentially, the static
keyword loads the following context into the method area
upon the loading phase, and such context then is shared and accessible across all threads
.
A static
variable
or a class variable
is shared across all instances within the scope of the same class
and hence can be used to contain class
specific data directly relevant to all instances. Otherwise, if combined with a public
access modifier
, the static
variables
then become accessible without class instantiation
, leading to memory efficiency
.
Example
public class Example {
private static int numOfInstances = 0;
...
public Example() {
numOfInstances++: // this info is then shared across all instances.
}
}
A static
method
is a method
that is specifically designed to be used without instantiating a class
containing such a static
method
. The helper functions such as Math.sqrt()
, Math.randon()
, or System.out.println()
are good examples of this. One noteworthy point for the static
method
is that it can only access or manipulate the static
variables
or static
methods
if within the same class.
Example
public class Example {
private static int numOfInstances = 0;
...
public static void printNumOfInstances() {
System.out.println("num: " + numOfInstances);
}
}
public class Main() {
public static void main() {
Example.printNumOfInstances(); // "num: (numOfInstances)"
}
}
A static
nested class
is specifically introduced for memory efficiency in which if a nested class
has not been statically
declared, memory
related issues like memory leak
, or out of memory
exceptions
can occur.
The use of static
keyword can result in:
memory
allocation.overhead
.the above positive aspects come at the expense of the following tradeoffs:
π¨βπ¨ increased complexity
πͺ low testability & maintainability
π reduced flexibility (static method)
- followed by no method overriding
(still compiles but won't run as intended)
These tradeoffs potentially violate the principles of OOP
and hence using the static
keyword has to be very carefully addressed.
The final
keyword is also critical in Java
programming and can be applied at the class
, variable
, and method
levels. The final
keyword, from the high-level point of view, declares any declared contexts to be unmodifiable.
A final
variable
is commonly used to declare constant variables with unchanging values if the type of a variable happens to be primitive types
in Java
. For reference types
, or objects
, the final
keyword can be used to declare such objects
to be immutable after the first initialisation.
Example
public class Example {
// these variables are now unmodifiable.
private final double count = 0;
// object requires constructor to instantiate the object.
// or can be instantiated right away.
private final OtherClass object;
...
}
}
A final
method
prevents overriding
in a subclass
. This is useful when you want to preserve the behavior of a method
as defined in the parent class
. Using final
methods
contributes to enhancing the stability of the code.
A final
class
prevents inheritance
. This is useful when you want to maintain the functionality of a specific class
as it is, without extending it. Using final
classes
reduces code complexity and makes maintenance easier.
Example
// String class is a good example
public final class String extends Object implements Serializable, Comparable<String>, CharSequence {
...
}
The use of final
keyword can result in:
π³οΈ consistent & reliable codes
- followed by the final
induced immutability.
π reduced complexity & resulting maintainability
π bug prevention
the above positive aspects come at the expense of the following tradeoffs:
inheritance
and method overriding
Overall, followed by the consistency and immutability after the final
keyword, developers, if appropriately use the keyword, become able to develop more consistent and reliable software.
The combination of the static
and final
keywords offers greater benefits, in which if appropriately used, increased code reusability and stability could be well anticipated.
Specifically, any contexts declared with static
and final
keywords enable such context to be immutable or unmodifiable and accessible across all functioning threads
. It is specifically practical for contexts like PI (3.14....)
, or any system variables.