News

How to Use ‘Static’ in Java

Static in java

Normally a class member must be accessed in conjunction with an object of its class. There will be times when you will want to define the class member which can be accessed without instantiating that class. This can be accomplished by ‘static’. To create such a member, precede its declaration with the keyword ‘static’.

Advantages of Static

When a member is declared static, it can be accessed before any objects of its class are created or without reference to any object.

Why do we declare main() as Static?

main() is the entry point of  a class. In java everything thing is written in a class. Now when you run java filename on command prompt, loader will load the class and JVM will search the main method to enter into the class. So making the main() as static, will make JVM access it directly through classname.main()

That’s why the program name must be same as the class name, where we wrote the main function.

Important Points to Remember 

  1. Instance Variables declared as static are essentially global variables. When you create objects of its class, no copy of a static variable is made. Even all objects share the same static variable.
  2. static variables or member functions will load during class. That means before creating any instances (objects), the main function is the first runnable function of any program which we run manually.
  3. If you want to get something executed/initialization of static variables before actual loading of the class, then put all those in static block.

E.g.

    static{

       System.out.println(“Static block initialized…”);

    }

It gets executed even before main() method during loading of the class.

Restrictions While using Static

  1. static method can access only static variables.
  2. static method can invoke static method only.
  3. static method can’t refer to this and super in any way.

Program

class Addition{
  static int num1;
  static int num2;
  int a;
  int b;
  Addition(){
    num1 = -1;
    num2 = -1;
    a = -1;
    b = -1;
  }
  Addition(int num1,int num2,int a,int b){
    this.num1 = num1;
    this.num2 = num2;
    this.a = a;
    this.b = b;
  }
  static void sum(){
    //System.out.println("Sum of 4 numbers is: "+(num1+num2+a+b));  /*static method can’t access static vars */
    System.out.println("Sum of 'num1' and 'num2' is: "+(num1+num2));
    System.out.println();
  }
  void add(){
    System.out.println("Addition of 4 numbers is: "+(num1+num2+a+b));
    System.out.println();
  }
}

class AditionDemo1{
  public static void main(String args[]){
    System.out.println();
    System.out.println("Static method sum() called without creating any object");
    System.out.println("------------------------------------------------------");
    Addition.sum();

    Addition ad1 = new Addition();

    System.out.println("Static Method sum() is invoked second time");
    System.out.println("-----------------------------------------------");
    Addition.sum();

    System.out.println("Non-Static Method add() is invoked using Object ad1");
    System.out.println("-----------------------------------------------");
    ad1.add();

    Addition ad2 = new Addition(10,20,30,40);

    System.out.println("Static Method sum() is invoked second time");
    System.out.println("-----------------------------------------------");
    Addition.sum();

    System.out.println("Non-Static Method add() is invoked using Object ad2");
    System.out.println("-----------------------------------------------");
    ad2.add();

    System.out.println("Static Method sum() is invoked third time");
    System.out.println("-----------------------------------------------");
    Addition.sum();

    System.out.println("Static Method sum() is invoked using Object");
    System.out.println("-------------------------------------------");
    ad1.sum();
    ad2.sum();
  }
}

Output

 Static in java

Explanation of the Java Code & Output

If you see the code carefully, then you will find we have taken two static variables and two non static variables which are initialized by two constructors viz. one is parameterized and another one is default. We have defined one static method and one non static method.

In main(), first we have invoked static method sum() without creating any object of Addition Class.

In the code you can see that static method can’t access non- static variables. If you try to access then you will get the following error, as we got:

Static in java

Now if you see, static variables are global variables only. Irrespective of the objects it has given the same result whenever static method sum() is invoked by any objects.

Non-static method can access static variables.

Non static method can invoke static methods.

Static methods can be invoked with <classname>.<static method name> and with <objectname>.< static method name > as well.

For non-static variables, each object will have different copies of non-static variables with different values for different objects.

Next we will learn about how to use “this” keyword in Java.

Checkout more useful tutorials and definitive guidelines on Java programming here.

1 Comment

Leave a Reply

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

To Top