DHA SUFFA UNIVERSITY
Department of CyberSecurity
CS-1005L- Object Oriented Programming
Spring 2025
LAB 05
More About OOP
Objective:
To learn about static keywords static methods and this keyword
Static Keyword:
The static keyword in Java is used for memory management mainly. Static is a non-access modifier in
Java which is applicable for the following:
1. blocks
2. variables
3. methods
4. nested classes
To create a static member (block, variable, method, nested class), precede its declaration with the keyword
static. When a member is declared static, it can be accessed before any objects of its class are created, and
without reference to any object. Static members belong to the class instead of a specific instance, this
means if you make a member static, you can access it without object. Static members are common for all
the instances (objects) of the class but non-static members are separate for each instance of class.
Restrictions for the static method
There are two main restrictions for the static method. They are:
1. The static method cannot use non-static data members or call non-static methods directly.
2. This and super cannot be used in a static context.
Static Variable:
A static variable is common to all the instances (or objects) of the class because it is a class level variable.
In other words you can say that only a single copy of a static variable is created and shared among all the
instances of the class. Memory allocation for such variables only happens once when the class is loaded
in the memory. Static blocks and static variables are executed in order they are present in a program.
● Static variables are also known as Class Variables.
● Unlike non-static variables, such variables can be accessed directly in static and non-static
methods.
● The static variable can be used to refer to the common property of all objects (which is not unique
for each object), for example, the company name of employees, college name of students, etc.
DHA SUFFA UNIVERSITY
Department of CyberSecurity
CS-1005L- Object Oriented Programming
Spring 2025
Example 1
Output:
Static Methods
Static Methods can access class variables (static variables) without using objects (instance) of the class,
however non-static methods and non-static variables can only be accessed using objects.
Static methods can be accessed directly in static and non-static methods. If you apply a static keyword
with any method, it is known as a static method. A static method can access a static data member and can
change the value of it.
DHA SUFFA UNIVERSITY
Department of CyberSecurity
CS-1005L- Object Oriented Programming
Spring 2025
Example 2
DHA SUFFA UNIVERSITY
Department of CyberSecurity
CS-1005L- Object Oriented Programming
Spring 2025
Output
Static Block
Static block is used for initializing the static variables. This block gets executed when the class is loaded
in the memory. It means that it is executed before the main method at the time of class loading. A class
can have multiple Static blocks, which will execute in the same sequence in which they have been
written into the program.
Example 3
DHA SUFFA UNIVERSITY
Department of CyberSecurity
CS-1005L- Object Oriented Programming
Spring 2025
Output
Static Class
A class can be made static only if it is a nested class. Nested static class doesn’t need reference of Outer
class A static class cannot access non-static members of the Outer class.
We will see examples of static class when we study the concept of nested class. Let’s see example of Static
Members (static variables, methods and blocks).
DHA SUFFA UNIVERSITY
Department of Data Science
CS-1005- Object Oriented Programming
Spring 2025
Example 4
Output:
Java Scanner Class
DHA SUFFA UNIVERSITY
Department of Data Science
CS-1005- Object Oriented Programming
Spring 2025
Lab Task:
Problem 01
Create a class Author whose attributes are: name, email, gender and books. Author
constructor initializes the values of name, email, gender and books. Define a static variable,
method and block, and use this keyword in your code.