Static Variables and Methods


Understanding static

There will come a time when you will want to define a class member that will be used independently of any object in the class. Generally, a class member only has access to an object in his class. However, it is possible to create a member that you can use manually without specifying a specific instance. To create such a member, before declaring it with keyword static. When a member is declared static, it can be accessed before any object of its class is created and without reference to any object. You can declare both methods and variables as static. The most common example of a static member is the main () method. main () is declared static because an object must be called before it exists.

If instance variables are declared as static, they can be called global variables. When objects of its class are declared, no duplicate of any static variable is created. Alternatively, all instances in the class share the same static variable.

Methods declared as static have a number of limitations.


Why you use static variable

A static variable can share all the objects in that class. In the case of instance variables, each object has a separate space for that variable.

Area of circle using instance variable

Here instance variable (_pi) is initialized for all objects of class which is unnecessary. For this reason, memory is occupied for all objects. Just imagine there are thousands of objects in reality. So programming is not much efficient.

Program
class Main
{
    public static void main(String[] args) 
    {
      Circle c1=new Circle();
      c1.SetPiRadius(3.14,10);
      c1.AreaOfCircle();
      
      Circle c2=new Circle();
      c2.SetPiRadius(3.14,20);
      c2.AreaOfCircle();
      
      Circle c3=new Circle();
      c3.SetPiRadius(3.14,30);
      c3.AreaOfCircle();
    }
}
class Circle
{
    double _pi;
    int _radius;
    public void SetPiRadius(double pi, int radius)
    {
      this._pi=pi;
      this._radius=radius;
    }
    public void AreaOfCircle()
    {
     System.out.println("Area of Circle is : "+this._pi*this._radius*this._radius);
    }
}
Run


Area of circle using static variable

Here static variable (_pi) is shared by all objects. So this program is much more efficient. Even there are thousands of objects there is only one copy of the static variable (_pi) is available. So memory is less used than the instance variable.The same output will be given.

In object pair basis we use the static variable.

Program
class Main
{
    public static void main(String[] args) 
    {
      Circle c1=new Circle();
      c1.SetRadius(10);
      c1.AreaOfCircle();
      
      Circle c2=new Circle();
      c2.SetRadius(20);
      c2.AreaOfCircle();
      
      Circle c3=new Circle();
      c3.SetRadius(30);
      c3.AreaOfCircle();
    }
}
class Circle
{
    static double _pi=3.14;
    int _radius;
    public void SetRadius(int radius)
    {
      this._radius=radius;
    }
    public void AreaOfCircle()
    {
     System.out.println("Area of Circle is : "+_pi*this._radius*this._radius);
    }
}
Run

Static variable

First of all you have to initialize the value once in the static variables and the static variables always take the updated value. Consider the following example:

Program
class Main
{
    int _val;
    static int _sval=10;
    Main(int val, int sval)
    {
        this._val=val;
        _sval=sval;
    }
    public static void main(String[] args)    	 
    {
	Main obj = new Main(10,20);
        System.out.println(_sval);
    }
}
Run

Static Method

Static method of the same class cannot call instance method of the same class. If you have to call that method, then that method must be static or you have to call that method by creating an object of that class.

Program
class Main
{
    static int _sval=10;
    public static void Show()
    {
       System.out.println("_sval = "+_sval);
    }
    public static void main(String[] args) 
    {
	Show();
    }
}
Run

Program
class Main
{
    static int _sval=10;
    public void Show()
    {
       System.out.println("_sval = "+_sval);
    }
    public static void main(String[] args) 
    {
      Main m1=new Main();
      m1.Show();
    }
}
Run

If another class has a static method, then to call that method, you have to call that method by that class name.

Program
class Main
{
    public static void main(String[] args) 
    {
      StaticTest.Show();
    }
}
class StaticTest
{
    static int _sval=10;
    public static void Show()
    {
       System.out.println("_sval = "+_sval);
    }
}
Run