Site icon Experiences Unlimited

Static Members: Static Methods and Static Variables

Why do we need Static members?

There are situations in which the method’s behaviour does not depend on the state of an object. So, there will be no use of having an object when the method itself will not be instance specific.

Let us consider another situation where in we want to keep a count of all the instances instantiated from a particular class. Suppose we declare an instance variable to do the job, it won’t work. Its because the instance variables are initialised back to their default value each time a instance is created. So we need some varaible which will be independent of the instances created.

The answer to both the situations is to use the “static” modifiers, in other words static members.

What exactly are Static Variable and Methods?

Variables and methods marked static belong to the class rather than to any particular instance of the class. These can be used without having any instances of that class at all. Only the class is sufficient to invoke a static method or access a static variable. A static variable is shared by all the instances of that class i.e only one copy of the static variable is maintained.

class Animal
{
  static int animalCount=0;
  public Animal()
  {
    animalCount+=1;
  }
  public static void main(String[] args)
  {
    new Animal();
    new Animal();
    new Animal();
    System.out.println(“The Number of Animals is: “
      + animalCount);
  }
}

The output is – “The Number of Animals is 3“.

A static method cannot access non-static/instance variables, because a static method is never associated with any instance. The same applies with the non-static methods as well, a static method can’t directly invoke a non-static method. But static method can access non-static methods by means of declaring instances and using them.

Note: One of the mistakes most often made by new Java programmers is accessing instance variables from main() method.

How to go about Accessing them?

In case of instance methods and instance variables, instances of that class are used to access them.

.
.

But static members are not associated with any instances. So there is no point in using the object. So, the way static methods (or static variables) are accessed is by using the dot operator on the class name, as opposed to using it on a reference to an instance.

class Animal
{
  static int animalCount=0;
  public Animal()
  {
    animalCount+=1;
  }
  public static int getCount()
  {
    return animalCount;
  }
}
class TestAnimal
{
  public static void main(String[] args)
  {
    new Animal();
    new Animal();
    new Animal();
    System.out.println(“The Number of Animals is: “
      + Animal.getCount());
    /*
    Notice the way in which the Static method is 
    called using the class name followed by static method.
    */
  }
}

Remember that static methods can’t be overridden. They can be redefined in a subclass, but redifining and overriding aren’t the same thing. Its called as Hiding. One can read the article on Overriding Vs Hiding here.

Exit mobile version