Static data members

Data members declared with static keyword are known as static data members. These are mainly used to represent those properties which are common to every object. At the time of class loading a single copy is created for static data members, which is shared by all objects.
Memory division in a java program execution.In a java program execution memory is divided into three parts:

Stack: Stack is used to local variables of the methods.

Heap: Heap is used to store objects.

Class Area: Class area is used to store static data members.

Static data members are used to represent those properties which are common to every object.

Example

/**
* This program is used to show that static data members are
* used to represent those properties which are common to every object.
* @author CodesJava
*/class MCAStudent{//name and rollNo are not common for all students//so keep them as non-static data members.String name;int rollNo;//As course offered is same for all students//so keep it as static.String courseName ="MCA";//constructor
MCAStudent(String n, int r){
name = n;
rollNo = r;}//display all valuespublicvoid display(){System.out.println("Name = "+ name);System.out.println("RollNo. = "+ rollNo);System.out.println("Course Name = "+ courseName);System.out.println("");}}publicclass StaticExample1 {publicstaticvoid main(String args[]){//create object of MCAStudent class.
MCAStudent stu1 =new MCAStudent("jai", 6);
MCAStudent stu2 =new MCAStudent("sunil", 15);//method call
stu1.display();
stu2.display();}}

/**
* This program is used to show that static data members are
* used to represent those properties which are common to every object.
* @author CodesJava
*/
class MCAStudent{
//name and rollNo are not common for all students
//so keep them as non-static data members.
String name;
int rollNo;
//As course offered is same for all students
//so keep it as static.
String courseName = "MCA";
//constructor
MCAStudent(String n, int r){
name = n;
rollNo = r;
}
//display all values
public void display(){
System.out.println("Name = " + name);
System.out.println("RollNo. = " + rollNo);
System.out.println("Course Name = " + courseName);
System.out.println("");
}
}
public class StaticExample1 {
public static void main(String args[]){
//create object of MCAStudent class.
MCAStudent stu1 = new MCAStudent("jai", 6);
MCAStudent stu2 = new MCAStudent("sunil", 15);
//method call
stu1.display();
stu2.display();
}
}