DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Trending

  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • SSRF Prevention in C#: Securing File Imports From User-Supplied URLs
  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Static Classes in Java

Static Classes in Java

By 
Shubham Bansal
·
Updated Dec. 14, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
29.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

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. 

Java
 




x
51


 
1
class Employee {
2
    private String name;
3
    private String email;
4
    private String address;
5
    
6
    User(String name, String email, String address) {
7
        this.name = name;
8
        this.email = email;
9
        this.address = address;
10
    }
11
    public String getName() {
12
        return name;
13
    }
14
    public String getEmail() {
15
        return email;
16
    }
17
    public String getAddress() {
18
        return address;
19
    }
20
    
21
    static class Validator {
22
23
        boolean validateEmployee(Employee employee) {
24
            if (!isValidName(employee.getName())) {
25
                return false;
26
            }
27
            if (!isValidEmail(employee.getEmail())) {
28
                return false;
29
            }
30
            return true;
31
        }
32
        private boolean isValidName(String name) {
33
            if (name == null) {
34
                return false;
35
            }
36
            if (name.length() == 0) {
37
                return false;
38
            }
39
            return true;
40
        }
41
        private boolean isValidEmail(String email) {
42
            if (email == null) {
43
                return false;
44
            }
45
            if (email.length() == 0) {
46
                return false;
47
            }
48
            return true;
49
        }
50
    }
51
}



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.

Java (programming language)

Published at DZone with permission of Shubham Bansal. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook