create your own

Introduction to Static Classes in Java

74
rate or flag this page

By nicomp

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

Java How to Program: Early Objects Version (8th Edition) Java How to Program: Early Objects Version (8th Edition)
Price: $78.90
List Price: $123.00
Java How to Program, 7th Edition Java How to Program, 7th Edition
Price: $39.99
List Price: $127.00
Java for Programmers Java for Programmers
Price: $33.48
List Price: $59.99
Java How to Program: Late Objects Version (8th Edition) Java How to Program: Late Objects Version (8th Edition)
Price: $49.00
List Price: $123.00

Software Engineering Poll

Static classes in Java are

  • A cheap shortcut that should be avoided at all costs.
  • A useful tool for implementing the Singleton design pattern.
  • Somewhere in the middle.
See results without voting

Comments

RSS for comments on this Hub

Meow Man  says:
6 weeks ago

Java static classes rock

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


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

  • Moon

    Moon is a film by Duncan Jones, the son of David Bowie, and his first film shows promise that he will may make a similar impact in the world of film as his father did in the world of music.Continue reading about "Moon" on New Earth Online - 2 days ago

  • Privacy on the World Wide Web

    No one likes their privacy being invaded, and whilst on the web it should not be an exception, so you should implement a P3P policy to give customers satisfaction that you respect their privacy. It will also give them some indication of what data you are collecting, or plan to collect.Continue reading about "Privacy on the World Wide Web" on New Earth Online - 2 days ago

  • Sony Handycam HDR-TG7VE

    If you want to start recording your own video for whatever reason you may have, such as uploading to any one of the many video uploading sites out there (YouTube, Vimeo, Flickr, etc.) you will no doubt want a decent High Definition camcorder to do the job. There are many good ones out there from Canon, Sony, Red, and others; though none seem to have good quality in such a small package as the Sony HDR-TG7VE.Continue reading about "Sony Handycam HDR-TG7VE" on New Earth Online - 3 weeks ago

  • Blade Runner

    Whether you loved it or hated it, there is no denying the fact that Ridley Scott's Blade Runner had a lasting impact on the world of science fiction. The book it was based upon, Philip K. Dick's "Do Androids Dream of Electric Sheep" is a respected novel which many say was superior to the film. What is it that people love and hate about the film though?Continue reading about "Blade Runner" on New Earth Online - 4 weeks ago

  • Dragon Age: Origins

    Bioware have a long history of award winning RPG games such as Baldur's Gate, Neverwinter Nights, Knights of the Old Republic, Jade Empire and the more recent Mass Effect. With an impressive back catalogue like this it is no wonder that all eyes are on Bioware every time they announce or release a new game. Does Dragon Age: Origins stand up to the test?Continue reading about "Dragon Age: Origins" on New Earth Online - 4 weeks ago

  • My Neighbor Totoro

    This 1988 anime film was another of the works by the great master of animation, Hayao Miyazaki. The latest release may be distributed by Walt Disney but it shouldn't put people off as it's been wonderfully dubbed for English audiences. If you purchase this film on DVD then the Japanese version is also on the same disc.Continue reading about "My Neighbor Totoro" on New Earth Online - 5 weeks ago

  • PHPEM 3: Cocoa from a PHP perspective

    Whether you're a PHP or a .NET developer it's becoming increasingly likely that you will at some point want to adapt your web application or site into a native mobile application that people can carry around with them. The transition to a new language isn't always easy especially in the case of Cocoa if you've not dealt with a language like it before. This article aims to help by demonstrating how you would achieve some of the tasks you'd normally do in PHP.Continue reading about "PHPEM 3: Cocoa from a PHP perspective" on New Earth Online - 6 weeks ago

  • Legacy Systems

    A legacy system is one which is one uses old technology and is still in current use due to dependencies that often make it hard to move away from. When it comes to upgrading or adding additional modules to a piece of software this can often pose many problems which can then raise the questions of how to proceed in a way that is both cost effective and beneficial to the company.Continue reading about "Legacy Systems" on New Earth Online - 2 months ago

working