0% found this document useful (0 votes)
59 views

OOP Lab Manual 7 - Static Methods &variables

This lab manual covers using static data members and static member functions in classes. It contains 3 stages: 1. The journey stage introduces static data members, which are shared among all class instances rather than being unique to each object. It also describes static vs non-static member functions. 2. The apply stage contains 3 activities demonstrating static variables being shared across objects, using a static variable to count objects, and declaring static methods. 3. The verify stage assigns 2 home activities - writing a program to count object creation/destruction using statics, and creating a Calculator class with static methods for arithmetic and trig functions.

Uploaded by

Dania Arshad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

OOP Lab Manual 7 - Static Methods &variables

This lab manual covers using static data members and static member functions in classes. It contains 3 stages: 1. The journey stage introduces static data members, which are shared among all class instances rather than being unique to each object. It also describes static vs non-static member functions. 2. The apply stage contains 3 activities demonstrating static variables being shared across objects, using a static variable to count objects, and declaring static methods. 3. The verify stage assigns 2 home activities - writing a program to count object creation/destruction using statics, and creating a Calculator class with static methods for arithmetic and trig functions.

Uploaded by

Dania Arshad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB

MANUAL
Course: CSC241-Object Oriented Programming

Department of Computer Science

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)

CCS241 –Lab Manual 1


COMSATS Institute of Information Technology (CIIT)
Islamabad
LAB # 07

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.

CCS241 –Lab Manual 2


1) Stage J (Journey)
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]
• e1.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]
• e1.getCount();
– [Class name].[public member function]
• Employee.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


– className.static_ method ();
– Object_of_class.static_method();
• Non static member function can access static and non static data member of class
– Object_of_class.Non_static_ method ();

CCS241 –Lab Manual 3


2) Stage a1 (apply)
Lab Activities:
Activity 1:
The following example demonstrates that static variables are common for all instances:

Solution:
Public class ABC
{
static int Var1=77; String Var2; //Static integer variable
} //non-static string variable
Public class ABCRunner
{

public static void main(String args[])


{
ABC ob1 = new ABC (); ABC ob2 = new ABC (); ob1.Var1=88; ob1.Var2="I'm Object1"; ob2.Var2="I'
System.out.println("ob1 integer:"+ob1.Var1); System.out.println("ob1 String:"+ob1.Var2); System.out.pr
}

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(){
CCS241 –Lab Manual 4
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.

Public class ABC


{
Public static int i ; public String s;

Public static void displayStatic() //Static method


{
System.out.println("i:"+i);
}
Public void display()//non static method
{
System.out.println("i:"+i); System.out.println("s:"+s);
}
}
Public class ABCRunner
{
public static void main(String args[]) //Its a Static Method
{
ABC a = new ABC();
a.display(); ABC.displayStatic();
}}

CCS241 –Lab Manual 5


CCS241 –Lab Manual 6
3) Stage v (verify)
Home Activities:
Activity 1:
Write program to count the number of objects created and destroyed for a class using static data
members and static member functions

Activity 2:
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.

CCS241 –Lab Manual 7

You might also like