Static Class in Java
Tags: java
Published: 2020-11-09 07:43:47

Take a look at this article, we are going to study what is a static class in java and how we can create it. Later, we will discuss some implementation consideration and benefits of using a static class

Visit the original article: https://www.java8net.com/2020/11/static-class-in-java.html



Before starting our tutorial on the static class, we first want to briefly remind you that "What is static?",  Static is a keyword that can be used with class, variable, method, and block to create static members. Static members belong to the class instead of a specific instance, this means if you make a member static, .you can access it without an object.

A static member can be:

1. static variables,
2. static methods,
3. static block or static initialization block, and,
4. static class

Static classes are basically a way of grouping classes together in Java. Java doesn't allow you to create top-level static classes; only nested (inner) classes. For this reason, a static class is also known as a static inner class or static nested class. Let's have a look at how to define a static class in java. Let's have a look at how to define a static class in java. 


```
class Employee {
	private String name;
	private String email;
	private String address;
    
	User(String name, String email, String address) {
		this.name = name;
		this.email = email;
		this.address = address;
	}
	public String getName() {
		return name;
	}
	public String getEmail() {
		return email;
	}
	public String getAddress() {
		return address;
	}
    
	static class Validator {

		boolean validateEmployee(Employee employee) {
			if (!isValidName(employee.getName())) {
				return false;
			}
			if (!isValidEmail(employee.getEmail())) {
				return false;
			}
			return true;
		}
		private boolean isValidName(String name) {
			if (name == null) {
				return false;
			}
			if (name.length() == 0) {
				return false;
			}
			return true;
		}
		private boolean isValidEmail(String email) {
			if (email == null) {
				return false;
			}
			if (email.length() == 0) {
				return false;
			}
			return true;
		}
	}
}
 
```


**Points to be considered while writing a static class**

1. It can only be a nested or inner class only.
2. It can have any access modifier (private, protected, public, or default) like any other static member.
3. It can only access the static members of its enclosing class.
4. It cannot access the non-static members of its enclosing class directly. It can interact with the non-static member only through the object of its enclosing class only. 


**Benefits of Static class in java**

1. We can define the related or helper classes inside the class by making it static.
2. It can access the private member of the enclosing class through the object reference.
3. It provides a nice namespace for the nested class.
4. If the enclosing class gets updated, we are able to update the static class as well at the same location.
5. Classloader loads the static class in JVM only at the time of the first usage, not when its enclosing class gets loaded.

Visit the original article: https://www.java8net.com/2020/11/static-class-in-java.html



**Conclusion**

In this article, we have studied what is a static class in java, how to create it and it's benefits. We have created a User with UserValidator class to provide the facility to check whether it is a valid user or not. It is good to have helper classes as a static class inside the enclosing class.