Static Block, Static Variable and Static Method

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


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

    class B

    please correct me if i am wrong !!

  2. 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 .

    1. 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.

      Jitendra Zaa

  3. I:>javac non-static method myMethod() cannot be referenced from a
     static context
    1 error

    1. 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.

  4. Hi,

    I am new apex and visualforce. I have simple question. Why we write “static” keyword for test methods.

    Thanks in advance.

  5. I have a public static variable and two static methods say method_1 and method_2 in my apex class. Say the variable name is x. Now I have assigned a value to variable x in method_1. Now when I try to access the value for variable x in method_2, I get null even if method_1 is called at the time of page load and before method_2 gets called.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.