Saturday, December 19, 2015

Java static variables and methods with example

In this article we will discuss about static keyword that means static method and variables with examples.This keyword is very important in Core Java concept every Java Programmer should have depth knowledge on static keyword. How to use static keyword and when to use while developing programs.
First Let We Discuss Why do we need Static Variables?

There are situations in which the method’s behavior 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.

Static Variable:

Static Variable is used to refer the common property of all objects.Example Company name of the Employee,college name of student etc..

Advantage:

It makes your program memory efficient that means it saves memory.

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 initialized back to their default value each time a instance is created. So we need some variable which will be independent of the instances created.

The answer is 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.

Example:
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);
}
}
Output: 

The number of animal 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.

Then How to accessing them?

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

<objectReference>.<instanceVariable>
<objectReference>.<instanceMethod>

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.

Example:

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 redefining and overriding aren’t the same thing. Its called as Hiding.



Simple Example for Static :

class StaticDemo{
static int a=10;
static int b=20;
public static void main(String args[])
{
System.out.println("a="+a);
System.out.println("b="+b);
show();
}
public static void show()
{
System.out.println("a="+a);
System.out.println("b="+b);
}
}


No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...