Static Keyword in Java

Table of Contents

Static Keyword in Java

Before we jump into the concept of Access modifiers, let’s understand a slightly different kind of non-access modifiers.

There is the following list of Non-access Modifiers, in this tutorial Static keyword will be discussed only:

  • The static modifier for creating class methods and variables.
  • The final modifier for finalizing the implementations of classes, methods, and variables.
  • The abstract modifier for creating abstract classes and methods.
  • The synchronized and volatile modifiers, which are used for threads.

Static Keyword

The static keyword in Java helps a lot to the programmer when they don’t need to make changes to the value assigned once to the variable, method, or class.

So, here are some points that you should keep in mind every time while using a static keyword. Following is the point that you should keep in mind:

  • Within a class, you can directly call a static variable or method, while doing the same for an instance a variable needs couple of steps to be written. Have a look at the following code snippet, it’s not valid as you are trying to access an instance variable from a static method which the main method.
class Sample
{
  int a = 20;//instance variable
  public static void main(String args[])
   {
    System.out.println(a);
   }
}
  • But, if you add a static keyword in front of it and make the variable a static method, then it will be fine to be used, as now you are trying to access a static variable from a static method main.
class Sample
{
  static int a = 20;//static variable
  public static void main(String args[])
   {
    System.out.println(a);
   }
}

Now,let’s have a look of an example and see the takeaways from it.

class Simple
{ int a = 30; //instance variable
  static int a = 20;//static variable
  public void method1(){//codes of your choice}
  public static void method2(){//codes of your choice}
  static class Soup //static inner class
  {
    public static int c = 50;
  }
}
public class Sample
{
 public static void main(String args[])
   {
    Simple object1 = new Simple();//creating an object of class Simple
    System.out.println(object1.a);//calling instance variable using object created
    System.out.println(Simple.b);//calling static variable
    System.out.println(Simple.Soup.c);//calling static variable from static inner class
   }
}

In the example above, we have seen the way to create a static method as well as the static inner class. And also we have seen the difference in calling technique of instance and static variables or methods.

Why it is used and when it is used?

Now, if a variable or a method or a class is to be used multiple times but creating the instance variable every time we use it, will demand a huge amount of memory.

And if you don’t want to compromise with the execution speed of your program then you should definitely go with the usage of static keyword. By just using the keyword static, you allocated a cache memory for the same, and this helps to reduce the calling time of the variables.

It is used when a variable or a method or a class, is to be used multiple times and the programmer needs the same value multiple times as well.

Leave a Reply