Static Members
Static Members :
A class contains two sections. One declares variables and the other declares methods. These variables and methods are called instance variables and instance methods. This is because every time the class is instantiated a new copy of each of them is created. They are accessed using the objects ( with dot operator).
Static members are variables and methods that are common to all instances of a class. A static member is the one that is common to all objects and accessed without using a particular object. The member belongs to the class as a whole rather than the objects created from the class.
For example
Static int count;
Static intmax(intx, int y );
Program illustrations for static members
Class metheoperation
{
Static float mul(float x, float y);
{
Return ( x*y);
}
Static float divide ( float x, float y)
{
Return ( x / y );
}
}
Class mathapplication
{
Public static void main ( stringargs[])
{
Float b = mathoperation.mul(4.0, 5.0);
Float b = mathoperation.divide(2.0, 5.0 );
System.out.println(“b = “ +b);
}
}
Note :
The static methods are called using class names. Infact no objects have been created for use.
Static methods have several restrictions:
- They can call only other static methods.
- They can only access static data.
- They cannot refer to this or super in anyway.