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:

  1. They can call only other static methods.
  2. They can only access static data.
  3. They cannot refer to this or super in anyway.

Leave Comment

Important Topics

Title
OOPS
Data Types
JAVA
JVM
Command Line Args
Machine Neutral
Scope of Variables
Operators :
Generic Type Casting
IF Else
Switch Statement
The while statement
The do statement
The for statement
Classes, objects and methods
Constructors
Methods Overloading
Static Members
Nesting of methods
Inheritance : Extending a Class
Overriding methods
Overriding methods
Final variable and methods
Abstract class in Java
Visibility control