πŸ”‘ Static & Final Keywords

GunhoΒ·2024λ…„ 10μ›” 11일
1

Object Oriented Programming (OOP) & Java

λͺ©λ‘ 보기
4/29

β™ŸοΈ Static

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.

🌞 static Variable

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. 
    }
}

🌝 static Method

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)"
    }
}

🌚 static Nested Class

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:

  • πŸ€– increased memory efficiency
    - followed by the shared memory allocation.
  • πŸ‹οΈβ€β™€οΈ improved performance
    - followed by accessible without object instantiation leading to no 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.

☠️ Final

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.

πŸ„ final variable

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; 
    ...
    }
}

πŸ„β€πŸŸ« final Method

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.

πŸͺ¨ final Class

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:

  • πŸ™ reduced flexibility (static method)
    - followed by no 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.

πŸ•ΈοΈ Static & Final

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.


πŸ“š References

Flab (1)
Flab (2)
μžλ°”μ˜ μ‹ 

profile
Hello

0개의 λŒ“κΈ€