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

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

#java #programming #developer

Static Classes in Java
20.85 GEEK