Introduction to Static Classes in Java

Introduction

The static class plays an important part in Object Oriented Programming (OOP). This tutorial introduces basic concepts of static classes in the Java programming language.

Figure 01 illustrates how to create a basic static class. Simply put the keyword static in the declaration of the methods in the class. The declaration of the class itself requires no special decoration. The class can contain static and non-static methods and properties simultaneously.

Figure 01 - A simple static class
Figure 01 - A simple static class

Figure 02 contains a main() that references the static class. Note that the reference uses the class name, not an object (instance) name. An instance is not required.

An instance can reference a static member, but be sure to have a thorough understanding of the side effects.

Figure 02 - The static class is referenced from the main()
Figure 02 - The static class is referenced from the main()

Let's modify the static class. In Figure 03, the class has a new property, alpha, and a new method, SetAlpha(). SetAlpha() is public and alpha is private.

The SetAlpha() method references the private property alpha.

Note that alpha and SetAlpha() are not declared as static.

All is well in the main(); it still builds and executes properly. No changes are necessary.

Figure 03 - A property and a method, both non-static are added to the class.
Figure 03 - A property and a method, both non-static are added to the class.

In Figure 05 the main() is broken because it's illegal to reference a non-static method using a class reference. The converse is permissible, but that's not the point of this exercise..

Figure 04 - The main() is broken because a non-static method cannot be accessed from a class reference.
Figure 04 - The main() is broken because a non-static method cannot be accessed from a class reference.

OK, let's try to fix the error by declaring the SetAlpha( ) method static. Figure 05 illustrates the attempt.

Figure 05 - An attempt to declare a method as static
Figure 05 - An attempt to declare a method as static

That fails. The SetAlpha( ) method cannot be declared static because it references a non-static variable, alpha.

Figure 06 illustrates the only way to fix the problem. The declaration of alpha must be modified to include the keyword static.

However, although the project now builds properly, we have created a situation in which only one instance of the variable alpha will be created when the program runs.

Figure 06 - The variable alpha is declared as static
Figure 06 - The variable alpha is declared as static

Static classes are commonly used to create classes that do not need to be instantiated and will be shared as a single copy throughout the entire project. 

The static class is also a first step in creating a design pattern called a Singleton.

Software Engineering Poll

Static classes in Java are

See results without voting

More by this Author


Comments 2 comments

anonymous 4 years ago

Those are static methods on a top level class.

Static classes are a completely different thing, and only inner classed can be static.

You article is wrong and misleading.

http://docs.oracle.com/javase/specs/jls/se7/html/j...

"Member classes may be static, in which case they have no access to the instance variables of the surrounding class; or they may be inner classes (§8.1.3)."


anonymous 4 years ago

yeah, all of them are static methods. Where is the part telling about static class? none! misleading!

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working