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

Posted

in

by

Tags:


Related Posts

Comments

14 responses to “Static Block, Static Variable and Static Method”

  1. Muzaffar Shah Khan Avatar
    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 !!

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

  3. Daniel Avatar
    Daniel

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

  4. Knowledge_the_power Avatar
    Knowledge_the_power

    which is loaded first ? static block or static method?

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

    2. Guest Avatar
      Guest

      static block

  5. surekha Avatar
    surekha

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

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

  6. venkat Avatar
    venkat

    Hi,

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

    Thanks in advance.

  7. abcd993 Avatar
    abcd993

    so you mean static block will be loaded to stack before the main method?

  8. Atul Avatar
    Atul

    C# don’t have static blocks

    1. JitendraZaa Avatar
      JitendraZaa

      This article is not about C#

  9. sumittiwari300694 Avatar

    Thank you for the valuable post about static method as i was in confusion but now its clear , It is going to help me a lot. Hope you will keep on posting to update us

  10. Pratik Ghosh Avatar
    Pratik Ghosh

    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 Reply to surekhaCancel reply

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

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading