Skip to main content

Static Class in Java

Static class in Java

In this article, we are going to study what is a Static class in java, how we can create it and the benefits of making 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 in java which is used to define a static member. A static member-created only once for a class and referred directly by the class name itself, objects created by the class can also refer the static member but all the objects share the same reference as static member-created only once for a class.
A static member can be:
  • static variables,
  • static methods,
  • static block or static initialization block, and,
  • static class
In this article, we will learn all about the static class in java, and we will discuss the other static members in another article. So, let's start this tutorial with the definition of the static class.

What is a Static class in Java?

As we have already studied, a static class is a static member of another class i.e., a static class in java can only be a nested class inside the other class. For this reason, a static class is also known as a static inner class or static nested class.
Click here to learn about inner class in java.
Let's have a look at how to define a static class in java.
class OuterClass{
	/*
	 * Body of the outer class
	 */
	
	static class InnerClass {
		/*
		 * Body of the InnerClass
		 */
	}
}

How to create a Static Class in Java?

Below, is the example of the User class which stores the details of a user and this class also contains a UserValidator class as a static class to validate the users.

Java Static Class Example 1: Let's have a look at an example to create a static class and how to use it

class User {
	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 UserValidator {
		private final String EMAIL_REGEX = "[-.\\w]+@[-.\\w]+.[A-Za-z]{2,4}";
		private final String NAME_REGEX = "[A-Za-z\\s]+";

		boolean validateUser(User user) {
			if (!isValidName(user.getName())) {
				return false;
			}
			if (!isValidEmail(user.getEmail())) {
				return false;
			}
			return true;
		}
		private boolean isValidName(String name) {
			if (name == null) {
				return false;
			}
			if (name.length() == 0) {
				return false;
			}
			if (!name.matches(NAME_REGEX)) {
				return false;
			}
			return true;
		}
		private boolean isValidEmail(String email) {
			if (email == null) {
				return false;
			}
			if (email.length() == 0) {
				return false;
			}
			if (!email.matches(EMAIL_REGEX)) {
				return false;
			}
			return true;
		}
	}
}
Above, we have created a class User which contains a static class named as UserValidator and the UserValidator class is used to validate the user.
As, we can see UserValidator is defined in the User class i.e., to access the UserValidator class we need to refer it from User class like User.UserValidator, it provides the UserValidator class a nice namespace. Also, if any modification needs to be done on the User class like adding/removing/updating any property, we can update the UserValidator at the same location as well.
Now, it's time to have a look at how to create the instance of this static class in java and use it.
class UserValidatorDemo {
	public static void main(String[] args) {
		UserValidator userValidator = new User.UserValidator();

		User user = new User("Shubham Bansal", "shubham.bansal@gmail.com",
				"Rohini, Delhi");

		if (userValidator.validateUser(user)) {
			System.out.println("Valid User");
		} else {
			System.out.println("Invalid User");
		}
	}
}
Output
Valid User

Points to be considered while writing a Static Class in Java

  • A static class can only be a nested or inner class only.
  • As, the static class is also a static member of its enclosing class, so it can have any access modifier (private, protected, public or default) like any other static member.
  • A static class can only access the static members of its enclosing class.
  • A static class 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. The access modifier does not matter here, it can access the private member as well.

Benefits of making Static Class in Java

  • We can define the related or helper classes inside the class by making it static.
  • By making a static class, we can access the private member as well through the object reference.
  • It provides a nice namespace for the nested class as we have seen above in the article as User.UserValidator 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 of the static class, 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 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.
Thanks for reading this article.
If, we missed something please let us know in the comment section below.
Happy Learning

Related Articles

Inner Class in Java
Immutable Class in Java

Comments