OOP Lab Manual 7 - Static Methods &variables
OOP Lab Manual 7 - Static Methods &variables
MANUAL
Course: CSC241-Object Oriented Programming
Learning Procedure
1) Stage J (Journey inside-out the concept)
2) Stage a1 (Apply the learned)
3) Stage v (Verify the accuracy)
4) Stage a2 (Assess your work)
Statement Purpose:
This Lab will help students in understanding the concept static data and member function of a
class.
Activity Outcomes:
After completion of this Lab students know in which scenario static data members, static
functions and static objects can are used.
Instructor Note:
The student should have understanding about access specifiers.
The students should understand the concept of object’s address space in memory.
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.
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
Solution:
Public class ABC
{
static int Var1=77; String Var2; //Static integer variable
} //non-static string variable
Public class ABCRunner
{
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 {
Public NoOfObjects(){
CCS241 –Lab Manual 4
objs++;
}
Public NoOfObjects(intx){
a=x;
objs++;
}
NoOfObjects o1=newNoOfObjects();
NoOfObjects o2=newNoOfObjects(122);
NoOfObjects o3=newNoOfObjects(150);
Activity 2:
Create a Calculator class that has following methods:
The user should be able to call these methods without creating an object of Calculator class.