Page 1 of 1

Basics of static variables/methods/classes/blocks Rate Topic: -----

#1 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Posted 03 March 2008 - 08:47 AM

hi,

I will be briefing what a static variable/method/class/block means.

A static literally means stand still. In programming the implication is a variable/method/class/block which is complete in itself i.e. no instantiation is required for using the element.

A variable is complete in itself means that you don't need any object to acces the static field. There are shared by all the objects of the class and hence they can be accessed by the class name as shown below:

class test{
    static int i = 0;
}

public class tester{
public static void main{
    System.out.println(test.i);
}
}



But Sun thought that it would be nice to allow the static variables to be accessed by objects also. So a code example to show this is:
class test{
    static int i = 0;
}

public class tester{
public static void main{
    test t = new test();
    System.out.println(t.i);
}
}



A code sample illustrate that the same copy of static variable is used among the objects of the class is as:
class test{
    static int i = 0;
    public test() {
        i++;
    }
}

public class tester{
public static void main{
    test t1 = new test();
    System.out.println(t1.i);
    test t2 = new test();
    System.out.println(t2.i);
    }
}



The output of the above code will be
1 2
which proves that same static variable is used for all the objects of the class.

A method is complete in iteslf has the implication of a variable that it can be used without creating any instance of the class. Here is a code sample to illustrate this:

class test{
    static int testMethod(){
        System.out.println("tested static");
    }
}

public class tester{
public static void main{
    System.out.println(test.testMethod());
}
}



As in case of variables, the static methods can also be accessed by using the objects.
main is the most common example of a method being static.
Please note that a static method can't access non-static members of the class and main method is no exception to it.

A static class is complete in itself means that its memebers can be accessed without creating any instance of the class. Thus in effect all the members of the class become static.
Please note that a top-level class can't be declared as static. The discussion of top-level and inner classes is a big topic in itself and more info about static classes can be found here


Even a code block can be static. But it has to be independent code block inside a class as shown below:
class test{
    static int i=0;
    static{
        i++;
    }
}

public class tester{
public static void main{
    System.out.println(test.i);
}
}



The output will be 1 because the static block is executed while the class is being loaded by the JVM.

Is This A Good Question/Topic? 2
  • +

Replies To: Basics of static

#2 prashanthi_asn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-May 09

Posted 27 May 2009 - 12:24 AM

hi,
very useful,simple and understandable explanation about 'static'..
Was This Post Helpful? 0
  • +
  • -

#3 joezim007  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 110
  • Joined: 13-September 08

Posted 13 June 2009 - 08:45 AM

I just read this to find out about the static block thing. I had seen that in a few places and I was extremely confused. Thanks for filling me in.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1