Java Static Class: A step further in your Java programming journey

javaToday, it’s almost impossible to browse the internet or run applications if your computer doesn’t support Java.  Developed by Sun Microsystems back in 1995, Java has been around for more than a decade now. It is one of the most popular programming languages in the world due to its power, efficiency and most of all because it is easy to learn.  In this tutorial we will explore the concept of static classes in Java. We’re assuming that you have a basic understanding of programming (in any language), and that you have Java installed on your computer. If you don’t have Java Run Time Environment installed (JRE), you can download it for free from the Sun Microsystems website.

To understand the concept of a static class, you must understand the basics of Java– which include classes and objects.

Classes in Java

Java is an object oriented language. A class in Java is just a collection of objects. It is a universal template out of which varying objects can be created. For example, if you take a real world object like a car, you know that the term “car” is a universal concept and there are different types of cars, like SUVs, sedans, trucks and luxury cars. You could create a class called “Car”, and its member objects might be SUVs, sedans, trucks and luxury cars. To declare a class, we use the syntax:

class Car {
 //code that may include field, method declaration and constructor
}

Here, the keyword “class” will instruct the compiler to create a collection of objects called “Car”.

Nested Classes

Java supports the concept of Nested Classes. Nested Classes are classes that can be defined within the body of another class. Why use nested classes in Java? Nested classes allow us to group together a collection of logically related classes. It helps makes the code more readable and less complex. It also helps us implement the concept of encapsulation (data hiding) better. The code becomes easier to handle, it’s easier to spot mistakes and make changes where necessary.

 The classes defined within the body of the class are known as nested classes and the class they are defined in is called the outer class.  Nested classes are somewhat different than sub-classes. Sub-classes cannot access private members of its parent class, while nested classes can. So, in some cases, programmers prefer to use nested classes over sub-classes.

If we continue with our earlier example, we can see that with “Car” as our outer class, we can create one or more classes nested classes within it called “SUVs”, “sedans”, “trucks” and “luxury cars”. We can add member objects to the newly created classes. For “SUVs” class, perhaps we can add specific types of SUVs, or SUVs with different brand names.  To declare a class within an existing class, we use the syntax:

class Car {
   //code
class SUV {
   //code
}
class sedans{
   //code
}

Static Classes in Java

In Java, only nested classes can be declared as static classes. If we attempt to declare a top-level class as a static class, we will get an error.  What are static classes? Static classes are nested classes that act like top-level classes, even if they appear somewhere in the middle or the bottom of the program hierarchy (within a class). Static classes do not need the reference of the outer class. They act like an outer class within themselves. Normal non-static nested classes, on the other hand, need reference of the outer class.  In other words, static classes are independent of the outer class, while other inner classes are dependent on the outer class.

Declaring a Static Class

To declare a static class within our “Car” class, we use the syntax:

class Car {
   class SUVs {
   }
   static class sedans {
   }
}

Here, the keyword “static” ensures that the class sedan will be treated as an outer class.

Static classes can only access static members of an outer class, while non-static nested classes can interact with both static and non-static members of an outer class. This functionality allows static nested classes to interact with other parts of the program and provides code re-usability.

Declaring an Object within a Static Class

You can access a static class by using the name of its outer class. For example

car.sedans

You can create an object for the static class only by accessing it through the outer class (which is true for all nested classes). You can use the syntax:

car.sedans outdatedsedan = new car.sedans ();

Here, you are creating a new object through the use of the keyword “new”.

Implementing the Concept of Static Classes in Java

Here’s a simple program in Java that demonstrates a static class. Notice how the static class accesses a member of its outer class – this is only possible because of the use of the keyword “static”.

class Car{
  public static String message = “meant for transportation”;
  static class sedans {
     public void printMessage ()
     {
        System.out.println (“Cars are” + message);
     }
  }  
}

Class Main
{
   Public static void main (String args[]) {
      car.sedans text = new car.sedans();
      text.printMessage();
   }
}
Output: Cars are meant for transportation.

Here, we have declared message as a static string variable. Because it is static, the static class “sedans” is able to access it. When the compiler encounters the System.out.println line, it will print the “Cars are meant for transportation” line of text on screen.

Benefits of a Static Class

A static class, like other nested classes, can access the private variables and methods of its outer class. Static classes can be declared to remove restrictions on member classes of an outer class. If we think the outer class is too limiting and we want a member class to perform more functions, we use the “static” keyword to provide that added functionality. A static class can never be instantiated. Static classes can’t directly access non-static members of a class. It can interact with them only through an object reference. This is both a drawback and an advantage of a static class.

With that, we hope you’ve got a better understanding of static classes in Java, and are well on your way to writing your own Android app in Java, or even creating your first interactive Java based website!