Static Block, Static Variable and Static Method

Get articles everyday as a email directly to your inbox:
Click to publicize, if you like this article :

In java the static keyword leads to following important   concepts  , namely

1) Static Variable

2) Static Block

3) Static Method

Static   Variables

Static variables are the special type of variable which are created during the load time of class, or we can say when the class is instantiated. The syntax for declaring variable as static is as follows,

static Block Static Method Static Variable

Some important points regarding static variables are

  1. It should be accessed by class name.
  2. Only one instance is created and it is shared by all the data members.

This can be further explained as follows  . Consider an example consisting of static and Non static member

static Block Static Method Static Variable

In above example when class is instantiated multiple copies of instance variable k is created  where each copy would going to point a particular object at a  time, but in case of static only one copy is created and shared among all objects & methods. As shown in fig

static Block Static Method Static Variable

Static Block:
Static block’s are the block which are implicitly called when the class is instantiated that means when the instance of class is made. (by  using new operator)

Static Block:Static block’s are the block which are implicitly called when the class is instantiated that means when the instance of class is made. (by  using new operator)

static Block Static Method Static Variable

When the above program is executed , for each instance of class StaticBlockDemo static block is called , but keep in mind it is called only when the class is instantiated, not declared.

Static Methods:

Some points about Static method are

  1. Static methods are automatically created during load time of class.
  2. They are accessed by using class name.
  3. A single copy of static method is created and is shared among all other methods.
  4. The most important point “Static function cannot access non static methods/memeber”.

Declaration of static method is given as follows

static Block Static Method Static Variable

No related posts.

You can leave a response, or trackback from your own site.
  • surekha

    I:>javac nonStaticMethod.java
    nonStaticMethod.java:7: non-static method myMethod() cannot be referenced from a
     static context
        myMethod();
        ^
    1 error

    • JitendraZaa

      Hi Surekha,
      Can you explain what you want to say?

      The error is saying you have to create ovject and then access method via object OR declare method as static.

  • Knowledge_the_power

    which is loaded first ? static block or static method?

    • Anonymous

      Hi,
      The complete class is loaded in a single shot and then execution occurs as per sequence in which they are written.

      If you have SOP statements in Static blocks and Static Method. It will execute the statements of Static block only and to execute statement of static method you have to call it from somewhere. This doe not mean that Static block is loaded before method.

      Regards,
      Jitendra Zaa

  • Daniel

    Thanks from that good explanation. I found another article about that:
    http://goo.gl/MWwVP

  • Aashish

    yeah.u r right.
    but in this case u need to extend class A in class B.
    becoz it wil allow u to use the variable defined in class A .

  • Muzaffar Shah Khan

    Yes Truly
    More over the concept for Static is used widely ,is to use static variables whose value is required in different classes
    like::
    in class A
    static boolean isRunning=false;

    class B
    if()
    A.isRunning=true;

    please correct me if i am wrong !!