PSOOP - Lecture 4-24-25
PSOOP - Lecture 4-24-25
-Lecture 4
Compiled by Nikahat Mulla
AGENDA
2
INTRODUCTION TO ARRAYS IN JAVA
OR
int[] arr = {10, 20, 30, 40, 50}; // Inline initialization
OR
int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
Example: Printing a 2D Array
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
JAGGED ARRAYS IN JAVA
Definition:
A jagged array is an array of arrays with variable
column sizes.
Declaration & Initialization
int[][] jaggedArray = new int[3][]; // Declaring a jagged
array
jaggedArray[0] = new int[2]; // First row has 2
elements
jaggedArray[1] = new int[4]; // Second row has 4
elements
jaggedArray[2] = new int[3]; // Third row has 3
elements
EXAMPLE: PRINTING A JAGGED ARRAY
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.println();
}
ARRAY OF OBJECTS IN JAVA
Definition:
Arrays can store objects instead of primitive data
types.
Example: Student Class with Array of Objects
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println(name + " " + age);
}
}
ARRAY OF OBJECTS IN JAVA
public class Main {
public static void main(String[] args) {
Student[] students = new Student[3]; // Array of Student objects
students[0] = new Student("Alice", 20);
students[1] = new Student("Bob", 22);
students[2] = new Student("Charlie", 21);
UNDERSTANDING THE STATIC KEYWORD IN
JAVA
Definition:
The static keyword is used for memory management
in Java.
It applies to variables, methods, and blocks.
Key Features:
Belongs to the class rather than an instance.
Allocated only once when the class is loaded.
USING STATIC
static keyword can be used in three scenarios:
For methods
note
USING STATIC (CONTD…)
static variable
Belongs to a class
A single copy to be shared by all instances of the class
Creation of instance not necessary for using static variables
Accessed using <class-name>.<variable-name> unlike instance
variables which are accessed as <object-name>.<variable-name>
static method
It is a class method
Accessed using class name.method name
Creation of instance not necessary for using static methods
A static method can access only other static data & methods, and
not non-static members
note
STATIC VARIABLES & METHODS
Static Variable
Example
class Example {
static int count = 0; // Shared across public class Main {
all instances public static void main(String[] args) {
Example obj1 = new Example();
Example() { Example obj2 = new Example();
obj1.display(); // Output: Count: 2
count++;
obj2.display(); // Output: Count: 2
} }
}
void display() {
System.out.println("Count: " +
count);
}
}
USING STATIC (CONTD…)
class Student { The static studCount variable is
private int rollNo; initialized to 0, ONLY when the class is
first loaded, NOT each time a new
private static int studCount; instance is made
public Student(){
studCount++;
Each time the constructor is invoked, i.e.
} an object gets created, the static variable
studCount will be incremented thus keeping
public void setRollNo (int r){ a count of the total no of Student objects
created
rollNo = r;
}
public int getRollNo (int r){ Which Student? Whose rollNo? A
static method cannot access anything
return rollNo; non-static
}
public static void main(String args[]){
System.out.println(“RollNo of the Student is;” +
rollNo);
}
}
STATIC METHOD EXAMPLE
class MathUtils {
// Static method for addition
static int add(int a, int b) {
return a + b;
}
}