Understanding Static Concept

By: aathishankaran Printer Friendly Format    

            There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of static member is main (). main () is declared as static because it must be called before any objects exist.

            Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.

            Methods declared as static have several restrictions:

v     They can only call other static methods

v     They must only access static data.

v     They cannot refer to this or super in any way.

            If you need to do computation in order to initialize your static variables, you can declare a static block, which 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:

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);

  }

static {

  System.out.println(“Static block initialized.”);

  B = a *4;

}

public static void main (String args[]){

  mech(42);

  }

}

            As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a*4 or 12. Then main() is called, which calls meth(), passing 42 to x. the three println() statements refer to the two static variables a and b, as well as to the local variable x.

 It is illegal to refer to any instance variables inside of a static method.

 Here is the output of the program:

 Static block initialized.

x = 42

a = 3

b =12

             Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need only specify the name of their class followed by the dot operator. For example, if you wish to call a static method from outside its class, you can do so using the following general form:

            classname.method()

             Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non-static methods through object-reference variables. A static variable can be accessed in the same way by use of the dot operator on the name of the class. This is how java implements a controlled version of global functions and global variables.

            Here is an example. Inside main(), the static method callme() and the static variable b are accessed outside of their class.

class StaticDemo {

     static int a = 42;

     static int b = 99;

     static void callme() {

          System.out.println(“a = “ + a);

     }

}

class StaticByName {

     public static void main (String args[]) {

          StaticDemo.callme();

          System.out.println(“b =” + StaticDemo.b);

     }

}

Here is the output of this program:

            a = 42

     b = 99





Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Enough to understand basic concept.Thanks.
View Tutorial          By: Jyothylakshmy P u at 2012-09-18 05:35:48

2. It's a verbatim copy from the book "Java Comp
View Tutorial          By: XYZGuru at 2012-09-25 06:31:04

3. Very good. Excellent examples and easy to understa
View Tutorial          By: Fernando Basso at 2012-09-30 22:17:56

4. Thanks. A couple of typos, though:
- &quo

View Tutorial          By: Steve Parker at 2013-09-25 09:47:08

5. Excellent way to explain the details, now I unders
View Tutorial          By: SAUL perrusquia at 2016-10-05 03:07:31

6. AndroidSMap
View Tutorial          By: AndroidSMap at 2017-01-13 20:31:06

7. Arymxurdak
View Tutorial          By: Arymxurdak at 2017-01-26 09:55:45

8. KostyaExhat
View Tutorial          By: KostyaExhat at 2017-03-28 15:28:43

9. CeccilGuemy
View Tutorial          By: CeccilGuemy at 2017-08-22 03:37:14