What is nested static class in Java
Nested static class in Java is a static member of any top level class. Though you can make any class static in Java, but you can only make nested classes i.e. class inside another class as static, you can not make any top level class static. Those classes are called nested static classes. Since to create instance of any nested class you require instance of outer class but that is not required in case of static nested class in Java. You can have an instance of nested static class without any instance of outer class. Here is an example of static nested class in Java
Output: Example of Nested Static Class in Java
When to use nested static class in Java
Normally we make a class static in Java when we want a single resource to be shared between all instances and normally we do this for utility classes which are required by all components and which itself doesn't have any state. Sometime interviewer ask when to use Singleton vs Static Class in Java for those purpose,answer is that if its completely stateless and it work on provided data then you can go for static class otherwise Singleton pattern is a better choice.
When to make a method static in Java
We can make a method static in Java in following scenario:
1) Method doesn't depends on object's state, in other words doesn't depend on any member variable and everything they need is passes as parameter to them.
2) Method belongs to class naturally can be made static in Java.
3) Utility methods are good candidate of making static in Java because then they can directly be accessed using class name without even creating any instance. Classic example is java.lang.Math
4) In various designs pattern which need a global access e.g. Singleton pattern, Factory Pattern.
Disadvantage of static method in Java
There are certain disadvantages also if you make any method static in Java for example you can not override any static method in Java so it makes testing harder you can not replace that method with mock. Since static method maintains global state they can create subtle bug in concurrent environment which is hard to detect and fix.
Example of static class and method in Java
Static method in Java is very popular to implement Factory design pattern. Since Generics also provides type inference during method invocation, use of static factory method to create object is popular Java idiom. JDK itself is a good example of several static factory methods like String.valueOf(). Core Java library is also a great place to learn how to use static keyword in java with methods, variables and classes. Another popular example of static method is main method in Java.
1. java.util.Collections has some static utility method which operates on provided collection.
2. java.lang.Math class has static method for maths operations.
3. BorderFactory has static method to control creation of object.
4. Singleton Classes like java.lang.Runtime.
Caution : Static methods should not manage or alter any state. and now a funny question what would happen if you execute following code
will it throw NullPointerException in Java or print "electronic system"
Nested static class in Java is a static member of any top level class. Though you can make any class static in Java, but you can only make nested classes i.e. class inside another class as static, you can not make any top level class static. Those classes are called nested static classes. Since to create instance of any nested class you require instance of outer class but that is not required in case of static nested class in Java. You can have an instance of nested static class without any instance of outer class. Here is an example of static nested class in Java
public class StaticClass{
public static void main(String args[]){
StaticClass.NestedStaticClass ns = new StaticClass.NestedStaticClass();
System.out.println(ns.getDescription());
}
static class NestedStaticClass{
public String NestedStaticDescription =" Nested Static Class in Java";
public String getDescription(){
return NestedStaticDescription;
}
}
}
Output: Example of Nested Static Class in Java
When to use nested static class in Java
Normally we make a class static in Java when we want a single resource to be shared between all instances and normally we do this for utility classes which are required by all components and which itself doesn't have any state. Sometime interviewer ask when to use Singleton vs Static Class in Java for those purpose,answer is that if its completely stateless and it work on provided data then you can go for static class otherwise Singleton pattern is a better choice.
When to make a method static in Java
We can make a method static in Java in following scenario:
1) Method doesn't depends on object's state, in other words doesn't depend on any member variable and everything they need is passes as parameter to them.
2) Method belongs to class naturally can be made static in Java.
3) Utility methods are good candidate of making static in Java because then they can directly be accessed using class name without even creating any instance. Classic example is java.lang.Math
4) In various designs pattern which need a global access e.g. Singleton pattern, Factory Pattern.
Disadvantage of static method in Java
There are certain disadvantages also if you make any method static in Java for example you can not override any static method in Java so it makes testing harder you can not replace that method with mock. Since static method maintains global state they can create subtle bug in concurrent environment which is hard to detect and fix.
Example of static class and method in Java
Static method in Java is very popular to implement Factory design pattern. Since Generics also provides type inference during method invocation, use of static factory method to create object is popular Java idiom. JDK itself is a good example of several static factory methods like String.valueOf(). Core Java library is also a great place to learn how to use static keyword in java with methods, variables and classes. Another popular example of static method is main method in Java.
1. java.util.Collections has some static utility method which operates on provided collection.
2. java.lang.Math class has static method for maths operations.
3. BorderFactory has static method to control creation of object.
4. Singleton Classes like java.lang.Runtime.
Caution : Static methods should not manage or alter any state. and now a funny question what would happen if you execute following code
public class ElectronicSystem {
private static String category = "electronic system";
public static void main(String[] args) {
ElectronicSystem system = null;
System.out.println(system.category);
}
will it throw NullPointerException in Java or print "electronic system"
No comments:
Post a Comment