DEV Community

Cover image for Day 5 of My Selenium Java Class – Variables, String Methods & Static vs Instance
scindia bethuraj
scindia bethuraj

Posted on

Day 5 of My Selenium Java Class – Variables, String Methods & Static vs Instance

Trainer: Mr.Nantha
Day 5
Session Overview
Today’s session focused on strengthening core Java concepts that are extremely important for automation testing and interviews.

Topics Covered

• Variables & Types of Variables: We explored the concept of variables in Java, including the different types that are commonly used.

• Important String Methods: The session included a review of key String methods, emphasizing their significance and usage in Java programming.

• Static vs Instance Variables: We discussed the distinction between static and instance variables, clarifying their roles and how they differ in Java applications.

Variables in Java
In Java, a variable acts as a container that holds data during program execution. Variables allow you to store, modify, and retrieve information as your code runs.

Example:

int age = 25;
String name = "John";

In these examples:

• The words int and String are data types that specify what kind of data the variable can store.
• Age and name are the variables that store values of type int and String respectively.

Types of Variables in Java

  1. Local Variable • Declared inside a method. • Accessible only within the method where it is declared. • Does not have a default value; you must initialize it before use.

Example:

void display() {
int number = 10;
}

  1. Instance Variable

• Declared inside a class but outside any method.
• Each object of the class has its own copy of the variable.
• Accessing the variable requires creating an object of the class.

Example:

class Student {
String name;
}

  1. Static Variable • Declared using the static keyword inside a class. • Shared among all objects of the class. • Memory for the variable is allocated only once, regardless of how many objects are created.

Example:

class Student {
static String college = "GRD College";
}

All objects of the Student class share the same value for the college variable.

Important String Methods

In automation testing with Selenium, Strings are used frequently for storing locators, validating data, and working with URLs. Mastering String methods is essential for efficient Java programming.
Some commonly used String methods include:

length()

Returns the number of characters in a String.

Example:
String name = "Java";
System.out.println(name.length());

toUpperCase() / toLowerCase()

Converts all characters to upper or lower case.
Example:
name.toUpperCase();
name.toLowerCase();

equals()

Compares two strings for equality.

Example:
name.equals("Java");

contains()

Checks if a substring exists within the String.

Example:
name.contains("av");

charAt()

Returns the character at a specific index.

Example:
name.charAt(0); // J

substring()

Extracts a part of the String from the given indices.

Example:
name.substring(1,3);

*Static vs Instance Variables *

Understanding the difference between static and instance variables is a common interview topic. The table below summarizes the key differences:

Instance Variable Static Variable

Belongs to object Belongs to class
Requires object creation No object needed
Each object has a separate copy Shared among all objects
Memory allocated multiple times Memory allocated once

Example:
class Example {
int instanceVar = 10;
static int staticVar = 20;
}

If you create three objects of the Example class:

• Each object gets its own copy of instanceVar (three copies in total).
• There is only one shared copy of staticVar for all objects.

Key Takeaways
• Variables are containers for storing data in a program.
• Java supports three main types of variables: Local, Instance, and Static.
• Mastering String methods is crucial for automation testing and interview success.
• Static variables are shared among all objects, while instance variables are unique to each object.

Example

Step 1: Create a file named:
StudentDemo.java

Step 2: class StudentDemo {

// Instance variable
String name;

// Static variable
static String college = "GRD College";

public static void main(String[] args) {

    // Creating object (instance)
    StudentDemo s1 = new StudentDemo();
    s1.name = "John";

    // Using String methods
    System.out.println("Name length: " + s1.name.length());
    System.out.println("Uppercase: " + s1.name.toUpperCase());

    // Printing instance and static variables
    System.out.println("Student Name: " + s1.name);
    System.out.println("College Name: " + college);
}
Enter fullscreen mode Exit fullscreen mode

}

Step 3: Compile the Program (Using Command Line)

  1. Open Command Prompt
  2. Navigate to the folder where the file is saved:
    cd Desktop

  3. Compile the file:
    javac StudentDemo.java

If there is no error, it will create:

StudentDemo.class

If there is an error, it will show the line number.

Step 4: Run the Program
Now type:
java StudentDemo

Expected Output
Name length: 4
Uppercase: JOHN
Student Name: John
College Name: GRD College

Memory Management in Java
**
Built-In Memory Management System
**
Java has a built-in memory management system that is designed to efficiently handle data storage and cleanup during program execution. This automatic process ensures that memory is allocated and released as needed, reducing the risk of memory leaks and improving overall program stability.

Importance in Automation Scripts

Understanding Java’s memory management system is essential for writing efficient Selenium or Playwright automation scripts. A solid grasp of these concepts helps developers optimize their scripts for better performance and reliability, making test automation more effective.

*How Java Memory is Organized
*

Java divides memory into two main areas:

Memory Area - Purpose

Stack Memory - Stores local variables and method calls. Memory is temporary and automatically removed when a method ends.

Heap Memory - Stores objects and instance variables. Memory is shared among objects and managed by Garbage Collector (GC).

Stack Memory

Stores primitive local variables and references to objects.

Follows Last In First Out (LIFO) principle.

Memory is automatically released when the method finishes.

Static vs Instance Variables in Memory

Variable Type Memory Location Notes

Instance Variable Heap Each object gets its own copy

Static Variable Method Area (part of heap) Shared among all objects

Garbage Collector (GC)

Java automatically removes objects that are no longer referenced to free up memory.

Helps prevent memory leaks

Reduces programmer effort

Memory Flow Example in a Program
class Student {
String name; // Instance variable (Heap)
static String college = "GRD College"; // Static variable (Method Area)

void display() {
    int age = 20;  // Local variable in stack
    System.out.println(name + " - " + age);
}
Enter fullscreen mode Exit fullscreen mode

}

public class MemoryDemo {
public static void main(String[] args) {
Student s1 = new Student(); // Object in Heap
s1.name = "John";
s1.display();
}
}

Memory Map:

s1 → reference in stack, object in heap

name → stored in heap

age → stored in stack temporarily

college → stored in method area (shared among objects)

Key Takeaways

• Variables are containers for storing data in a program.

• Java supports three main types of variables: Local, Instance, and Static.

• Mastering String methods is crucial for automation testing and interview success.

• Static variables are shared among all objects, while instance variables are unique to each object.

Stack Memory

Stores local variables and method calls.

Memory is temporary and released automatically when the method ends.
Heap Memory

Stores objects and instance variables.

Shared among all objects. Memory is managed by Garbage Collector.
Instance vs Static Variables in Memory

Instance variables → stored in heap, each object has its own copy.

Static variables → stored in method area, shared among all objects.
Garbage Collector (GC)

Automatically removes unreferenced objects.

Prevents memory leaks and reduces manual memory management.

Top comments (0)