Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.3. Static members

4.3. Static members
Last updated: 27 January 2013.

In the previous tutorials, you have learned that objects are created from a class which is their code representation. Furthermore, each created object has its own copies of the instance variables and instance methods.

However, it is sometimes necessary to have a single copy of a variable that is shared by all objects. For example, you may need to store the total number of created Aircraft objects in a variable that would be incremented each time an object is created (each time a constructor is called). The keyword static allows you to declare such a variable. Here is an example:

public class Aircraft {

   
float length;
   
float height;
   
int range;
   
double weight;
   
boolean airborne;

   
static int totalAircrafts = 0;

   
public Aircraft(){
         
++totalAircrafts;
   
}


   
public Aircraft(float length, float height, int range, double weight){
         
this.length = length;
         
this.height = height;
         
this.range = range;
         
this.weight = weight;
          ++totalAircrafts;
   
}


   
public Aircraft(float length, float height, int range, double weight, boolean airborne){
         
this.length = length;
         
this.height = height;
         
this.range = range;
         
this.weight = weight;
         
this.airborne = airborne;
          ++totalAircrafts;
   
}


   
public boolean land(){
         
if(airborne == true){
               
airborne = false;
               
return true;
         
}
         
return false;
   
}


   
public boolean takeOff(){
         
if(airborne == false){
               
airborne = true;
               
return true;
         
}
         
return false;
   
}

}

totalAircrafts is declared as a static variable and, as a result, a newly created Aircraft object does not have its own copy of the variable totalAircrafts. Instead, totalAircrafts has a unique address in memory. In the example shown below, several Aircraft objects are created and the value of the variable totalAircrafts is printed to the standard output:

public class App {

   
public static void main(String[] args) {

         
Aircraft aircraft1 = new Aircraft();
          Aircraft aircraft2 =
new Aircraft(58.17F, 12.7F, 257, 17650.5D);
          Aircraft aircraft3 =
new Aircraft(58.17F, 12.7F, 257, 17650.5D, false);

          System.out.println
(Aircraft.totalAircrafts);

   
}

}

The output is:

3


Variables that are declared static can be accessed without creating an instance of the class. In the example shown above, I accessed the variable totalAircrafts directly via the name of the class:

Aircraft.totalAircrafts

This is the recommended way to access static variables (directly via the name of the class). I could have accessed the variable totalAircrafts via one of the instances aircraft1, aircraft2 or aircraft3:

aircraft1.totalAircrafts

Nonetheless, accessing static variables via instances is not recommended.

Static variables are also called class variables (as opposed to instance variables) because they are bound to the class instead of being bound to a particular instance of the class. Static fields will come in handy to declare constants that are shared by all the classes of an application. Take a look at the class AircraftConstants shown below:

public class AircraftConstants {

   
static final int MINIMUM_TAKEOFF_DISTANCE = 0;
   
static final int MAXIMUM_CREW_COUNT = 30;
   
//...
    //...
}

The constants listed in the class AircraftConstants can be accessed from any other class via the AircraftConstants class name:

AircraftConstants.MAXIMUM_CREW_COUNT

Similarly to static variables, you can declare static methods. The static method squareSurface shown below returns the surface of a square:

static double squareSurface(double sideLength){
   
return (sideLength * sideLength);
}

Generally, static methods are used as utility methods that can be called from all the classes of an application. For example, the method squareSurface shown above can be included in a class named GeometryTools which will possibly contain other utility methods:

public class GeometryTools {

   
static double squareSurface(double sideLength){
         
return (sideLength * sideLength);
   
}

   
// other static utility methods
    // ...

}

Static methods are called via the name of the class they belong to. In other words, no instance is needed to call static methods. The class App is modified to call the method squareSurface:

public class App {

   
public static void main(String[] args) {

         
System.out.println(GeometryTools.squareSurface(2.0));

   
}

}

The output is:

4.0

In the tutorial Hello World program, you have seen that the main method is the entry point of a Java application and it always has the same signature:

public static void main(String[] args)

At startup (when you type java App at the command prompt), the Java virtual machine looks for that signature. At that point, there is no instance of any class from your application in memory and therefore, the Java virtual machine cannot call any method unless it is declared static. For this reason, the main method is declared static so that the Java virtual machine can execute it without needing an instance of the class App.

Note that you can use static initialization blocks in a class. You have already seen initialization blocks in the previous tutorial. A static initialization block is like a normal initialization block except that it starts with the keyword static:

public class Aircraft {

   
float length;
   
float height;
   
int range;
   
double weight;
   
boolean airborne;

   
static int totalAircrafts = 0;

   
// This is a static initialization block
   
static {
         
System.out.println("Aircraft created");
   
}


   
public Aircraft(){
         
++totalAircrafts;
   
}


   
public Aircraft(float length, float height, int range, double weight){
         
this.length = length;
         
this.height = height;
         
this.range = range;
         
this.weight = weight;
          ++totalAircrafts;
   
}


   
public Aircraft(float length, float height, int range, double weight, boolean airborne){
         
this.length = length;
         
this.height = height;
         
this.range = range;
         
this.weight = weight;
         
this.airborne = airborne;
          ++totalAircrafts;
   
}


   
public boolean land(){
         
if(airborne == true){
               
airborne = false;
               
return true;
         
}
         
return false;
   
}


   
public boolean takeOff(){
         
if(airborne == false){
               
airborne = true;
               
return true;
         
}
         
return false;
   
}

}

The statements in a static initialization block are executed only once no matter how many instances are created. As an example, you can run the following statements:

Aircraft aircraft1 = new Aircraft();
Aircraft aircraft2 =
new Aircraft(58.17F, 12.7F, 257, 17650.5D);
Aircraft aircraft3 =
new Aircraft(58.17F, 12.7F, 257, 17650.5D, false);

The output is:

Aircraft created


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.3. Static members
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.4. Inheritance

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7