Static Data members and Methods
Introduction:
There is an important exception to the rule that each object of a class has its own copy of all the data members of the class. In certain cases, only one copy of a variable should be shared by all objects of a class. A static data member is used for such propose.. Such a variable represents “class-wide” information (i.e., a property of the class shared by all instances, not a property of a specific object of the class). The declaration of a static member begins with keyword static.
In the above figure, consider Object1, Object2 and Object3 are three of a class. Each object has its own private data members. The class has a static data member that is share among all the object of that class. This data member has it own memory allocated that is separate from the memory allocated for each object of that class.
Static data member – access
A public static data member can be access using any object of the class or class name
- [Object Name].[name of static variable]
- count;
- [Class Name] .[name of static variable]
- Employee .count;
A private static member can be access using a public member function
- [objectName].[public member function]
- getCount();
- [Class name].[public member function]
- getCount();
A static method is a method that can only access static data member of class. It cannot access non static data member of that class
Static vs. non static functions
- Static function can only access static data member of class
- static_ method ();
- static_method();
- Non static member function can access static and non static data member of class
- Non_static_ method ();
Lab Activities:
Activity 1:
The following example demonstrates that static variables are common for all instances:
Solution:
Output:
ob1 integer:88
ob1 String:I’m
Object1 ob2
integer:88
Activity 2:
The following example demonstrates counting of objects by using static variable.
Solution:
Public class NoOfObjects {
Private static int objs=0; Private int a;
Public NoOfObjects(){
objs++;
}
Public NoOfObjects(intx){ a=x;
objs++;
}
Public static int getObjs (){ return objs;
}
}
Public class NoOfObjectsRunner
{
Public static void main(String[] args){
NoOfObjects o1=newNoOfObjects(); NoOfObjects o2=newNoOfObjects(122); NoOfObjects o3=newNoOfObjects(150);
System.out.println(“Objects created:”+ NoOfObjects.getObjsCreated()); System.out.println(“Objects created:”+ o1.getObjsCreated());
}
}
Activity 3:
The following example demonstrates static methods declaration.
Home Activities:
Activity 1:
Create a SavingsAccount class. Use a static data member annualInterestRate to store the annual interest rate for each of the savers. Each member of the class contains a private data member savingsBalance indicating the amount the saver currently has on deposit. Provide member function calculateMonthlyInterest that calculates the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance.
Provide a static member function modifyInterestRate that sets the static annualInterestRate to a new value.
Write a driver program to test class SavingsAccount. Instantiate two different objects of class SavingsAccount, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set the annualInterestRate to 3 percent. Then calculate the monthly interest and print the new balances for each of the savers.
Then set the annualInterestRate to 4 percent, calculate the next month’s interest and print the new balances for each of the savers.
Activity 2:
Write program to count the number of objects created and destroyed for a class using static data members and static member functions
Assignment:
Create a Calculator class that has following methods:
sum, multiply, divide , modulus , sin , cos , tan
The user should be able to call these methods without creating an object of Calculator class.