Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
Reading time: 30 minutes
A variable is called static that always holds latest values and its value is shared among all the objects. For example if you have ten instances of the same class type then the data or methods will be shared by all ten. The instantiated objects will have their own versions of other data and methods,but only a single copy of the ones defined as static.
In this article, you will learn:
- What are static members in Java?
- When to use them? and When they can be a problem?
Static means only one copy exists for the entire class irrespective of the number of objects that exists for that class. Memory for the static member is created before the object is created, because of this they can be called only with the class name.
Syntax to declare a static variable:
static datatype variableName;
ex:
static int x=10;
The features of static variables:
-
Java instance variables are given separate memory for storage. If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declartion.
-
Any java object that belongs to that class can modify its static variables.4
-
Static methods can be accessed by java instance methods also.
the keyword static is used to declare static members for a class.
Static Methods
Static methods use no instance variables of any object of the class they are defined in. They can use class variables of the class without using any object name.They use class name instead for their invocation.
A static method is created by placing keyword static before their declaration.
Synatx for static method:
static returntype methodName()
{
...
}
How to call static methods?
The static methods can be called either from within their class of origin or from any other class. That is there are two cases:
- When called from within the same class, just write the static method name. Although you can invoke them just like instance methods but the former way is preferred one. That is the syntax for invoking a static method from within their own class:
Static_Method_Name(Parameters if any);
- When called from outside the class of origin, then use the following syntax
Class_Of_Origin.Static_Method_Name(parameters if any);
Let us consider an example:
public class Feet2Meters
{
public static final double feet2meter=0.3048; // static variable defined here
public static double convert(double feet) // static method to convert feet to meters
{
return feet*feet2meter;
}
public static void main(String args[])
{
double feet=100;
double meter=Feet2Meters.convert(feet);
System.out.println(feet+"feet is"+ meter+"meters.");
}
}
Output:
100.0 feet is 30.48 meters.
When to use static members?
Use static variable when you need something that will be used through out the application and every instance need to know the variable. Use static method to do some utility task. Can be called without any object declaration.
Example for static method:
class Difference {
public static void main(String[] args)
{
display(); //calling without object
Difference t = new Difference();
t.show(); //calling using object
}
static void display()
{
System.out.println("Programming is amazing.");
}
void show()
{
System.out.println("Java is awesome.");
}
}
Output
Programming is amazing.
Java is awesome.
In the above example we can directly call static methods without creating objects of that class, but we have to create an object to call the non static methods.
What are the problems with static members?
Static members are part of class and thus remain in memory till application terminates and can’t be ever garbage collected. Using excess of static members sometime predicts that you fail to design your product.It denotes that object oriented design is compromised. This can result in memory over flow.Also there are certain disadvantages if you make any method static in Java for example you can not override any static method in Java so it makes it difficult to apply object oriented programming principles.
Neither static methods nor static variables can access the non-static variables and methods of a class.But reverse is true:non-static methods and variables can access the static variables and methods because the static members of class exists even if no instances of class exist.
Consider the following code:
class MyClass
{
static int x=count();
int count()
{
return 10;
}
}
this gives a compilation error stating that non static method count can not be referenced from a static context.
The problem in overriding a static method:
// Base Class
class Parent
{
static void show() // here a static method is used and therfore we cannot
// override this method in Child class.
{
System.out.println("Parent's show()");
}
}
// Inherited class
class Child extends Parent
{
// This method overrides show() of Parent but we cant override it as its static
@Override
void show()
{
System.out.println("Child's show()");
}
}
// Driver class
class Main
{
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();
Parent obj2 = new Child();
obj2.show();
}
}
In the above example there are two classes, Parent and Child. Child class inherits the Parent class and hence it overrides the show() method of Parent class, but since the show() method of the Parent class is declared as static, the Child class cannot override it and gives a compilation error.