Static Classes in Java
Join the DZone community and get the full member experience.
Join For FreeIn 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 considerations and benefits of using a static class.
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.
However, if you well understand "What is a static class in java?" then you can skip this article and brush up your skills with how to create an inner class in java?.
A static member can be:
- static variables,
- static methods,
- static block or static initialization block, and,
- 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
- It can only be a nested or inner class only.
- It can have any access modifier (private, protected, public, or default) like any other static member.
- It can only access the static members of its enclosing class.
- 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
- We can define the related or helper classes inside the class by making it static.
- It can access the private member of the enclosing class through the object reference.
- It provides a nice namespace for the nested class.
- If the enclosing class gets updated, we are able to update the static class as well at the same location.
- Classloader loads the static class in JVM only at the time of the first usage, not when its enclosing class gets loaded.
Conclusion
In this article, we have studied what is a static class in java, how to create it and its 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.
Published at DZone with permission of Shubham Bansal. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments