Static Block, Static Variable and Static Method

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 12 Comments on Static Block, Static Variable and Static Method

Infrormation about Static Block, Static Variable and Static Method in JAVA

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

Related posts

    12 thoughts on “Static Block, Static Variable and Static Method”

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

    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.

        Regards,
        Jitendra Zaa

    3. I:>javac nonStaticMethod.java
      nonStaticMethod.java:7: non-static method myMethod() cannot be referenced from a
       static context
          myMethod();
          ^
      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.

    Leave a Reply

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