Java static keyword

Introduction

To define a class member that can be accessed independently of any object of that class, use the static keyword.

In this way we can access a member by class itself without reference to a specific instance.

When a member is declared static, it can be accessed without creating any objects of its class.

We can declare both methods and variables to be static.

Instance variables declared as static are, essentially global variables.

All instances of the class share the same static variable.

Restrictions for static

static Methods have several restrictions:

  • static method can only directly call other static methods.
  • static method can only directly access static data.
  • static method cannot refer to this or super.

static block

To initialize static variables, we can declare a static block.

The static block gets executed exactly once when the class is first loaded.

The following example shows a class that has a static method, some static variables, and a static initialization block:

// Demonstrate static variables, methods, and blocks.
class UseStatic {
  static int a = 3;
  static int b;

  static void meth(int x) {
    System.out.println("x = " + x);
    System.out.println("a = " + a);
    System.out.println("b = " + b);
  }/*  w w w. j  a  v  a2  s  .  c  om*/

  static {
    System.out.println("Static block initialized.");
    b = a * 4;
  }

  public static void main(String args[]) {
    meth(42);
  }
}

When the UseStatic class is loaded, all of the static statements are run.

First, a is set to 3, then the static block executes.

To call a static method from outside, use the following general form:

ClassName.method();

The following code calls the static method vis its class name.


class StaticDemo{
  static int a = 42;
  static int b = 99;
  static void callme() {
    System.out.println("a = " + a);
  }/*from   w  w w  .j a  v  a 2s. c om*/
}

public class Main {
  public static void main(String args[]) {
    StaticDemo.callme();
    System.out.println("b = " + StaticDemo.b);
  }
}

Java static keyword count instance


class Circle {/*  ww w .j a  va  2 s. co  m*/
  /** The radius of the circle */
  double radius;

  /** The number of the objects created */
  static int numberOfObjects = 0;

  /** Construct a circle with radius 1 */
  Circle() {
    radius = 1.0;
    numberOfObjects++;
  }

  /** Construct a circle with a specified radius */
  Circle(double newRadius) {
    radius = newRadius;
    numberOfObjects++;
  }

  /** Return numberOfObjects */
  static int getNumberOfObjects() {
    return numberOfObjects;
  }

  /** Return the area of this circle */
  double getArea() {
    return radius * radius * Math.PI;
  }
}

public class Main {
  public static void main(String[] args) {
    System.out.println("Before creating objects");
    System.out.println("The number of Circle objects is " + Circle.numberOfObjects);

    // Create c1
    Circle c1 = new Circle();

    System.out.println("\nAfter creating c1");
    System.out.println("c1: radius (" + c1.radius + ") and number of Circle objects (" + c1.numberOfObjects + ")");

    // Create c2
    Circle c2 = new Circle(5);

    // Modify c1
    c1.radius = 9;

    // Display c1 and c2 AFTER c2 was created
    System.out.println("\nAfter creating c2 and modifying c1");
    System.out.println("c1: radius (" + c1.radius + ") and number of Circle objects (" + c1.numberOfObjects + ")");
    System.out.println("c2: radius (" + c2.radius + ") and number of Circle objects (" + c2.numberOfObjects + ")");
  }
}



PreviousNext

Related