A method is a reusable block of code that performs a specific task. In automation, methods are everywhere — click(), sendKeys(), isDisplayed(), getTitle(). You will write your own methods to organize test logic.
public class MethodBasics {
// Method with no parameters, no return value
static void printSeparator() {
System.out.println("========================");
}
// Method with parameters and return value
static int add(int a, int b) {
return a + b;
}
// Method that returns a String
static String getGreeting(String name) {
return "Hello, " + name + "!";
}
public static void main(String[] args) {
printSeparator();
System.out.println(getGreeting("Tester"));
printSeparator();
int sum = add(10, 20);
System.out.println("Sum: " + sum);
}
}========================
Hello, Tester!
========================
Sum: 30| Return Type | Meaning | Example |
|---|---|---|
| void | Returns nothing | printSeparator() |
| int | Returns a whole number | add(5, 3) → 8 |
| double | Returns a decimal | calculateTax(100.0) → 8.0 |
| boolean | Returns true/false | isValid("test") → true |
| String | Returns text | getTitle() → "Dashboard" |
public class MethodParams {
// No parameters
static String getBaseUrl() {
return "https://demo-bank.example.com";
}
// One parameter
static boolean isValidEmail(String email) {
return email.contains("@") && email.contains(".");
}
// Multiple parameters
static String buildUrl(String base, String path, int id) {
return base + "/" + path + "/" + id;
}
public static void main(String[] args) {
System.out.println(getBaseUrl());
System.out.println(isValidEmail("user@test.com")); // true
System.out.println(isValidEmail("invalid")); // false
String url = buildUrl("https://api.example.com", "users", 42);
System.out.println(url); // https://api.example.com/users/42
}
}Overloading means having multiple methods with the same name but different parameter lists. Java picks the right one based on the arguments you pass.
public class Overloading {
// Log with message only
static void log(String message) {
System.out.println("[INFO] " + message);
}
// Log with level and message
static void log(String level, String message) {
System.out.println("[" + level + "] " + message);
}
// Log with level, message, and test name
static void log(String level, String testName, String message) {
System.out.println("[" + level + "] " + testName + " — " + message);
}
public static void main(String[] args) {
log("Test started");
log("WARN", "Slow response detected");
log("ERROR", "LoginTest", "Element not found");
}
}[INFO] Test started
[WARN] Slow response detected
[ERROR] LoginTest — Element not foundOverloading matches on parameter count and types, NOT on return type. Two methods with the same name and same parameter types but different return types will cause a compile error.
A variable exists only within the block (curly braces) where it is declared. This is called scope.
public class Scope {
static int classLevel = 100; // accessible everywhere in this class
static void demo() {
int methodLevel = 50; // accessible only in this method
System.out.println(classLevel); // OK
System.out.println(methodLevel); // OK
if (true) {
int blockLevel = 25; // accessible only in this if block
System.out.println(blockLevel); // OK
System.out.println(methodLevel); // OK
}
// System.out.println(blockLevel); // ERROR — out of scope
}
public static void main(String[] args) {
demo();
System.out.println(classLevel); // OK
// System.out.println(methodLevel); // ERROR — out of scope
}
}public class TestHelpers {
static boolean isEven(int number) {
return number % 2 == 0;
}
static boolean isPalindrome(String text) {
String cleaned = text.toLowerCase().replaceAll("[^a-z0-9]", "");
String reversed = new StringBuilder(cleaned).reverse().toString();
return cleaned.equals(reversed);
}
static String maskPassword(String password) {
if (password.length() <= 2) return "**";
return password.charAt(0) + "*".repeat(password.length() - 2) + password.charAt(password.length() - 1);
}
static int countOccurrences(String text, char target) {
int count = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == target) {
count++;
}
}
return count;
}
public static void main(String[] args) {
System.out.println(isEven(4)); // true
System.out.println(isPalindrome("racecar")); // true
System.out.println(isPalindrome("hello")); // false
System.out.println(maskPassword("Test@123")); // T******3
System.out.println(countOccurrences("hello world", 'l')); // 3
}
}Q: Is Java pass by value or pass by reference?
A: Java is always pass by value. For primitives, the actual value is copied — changes inside the method do not affect the original variable. For objects (including Strings and arrays), the reference (memory address) is copied by value — the method receives a copy of the reference, so it can modify the object's contents but cannot make the original variable point to a different object.
Q: What is method overloading?
A: Method overloading is defining multiple methods with the same name but different parameter lists (different number of parameters, different types, or different order). The compiler chooses the correct method at compile time based on the arguments. It is a form of compile-time polymorphism. Return type alone cannot differentiate overloaded methods.
Exercise 1: Write a method isPrime(int n) that returns true if n is a prime number. Test it with 2, 7, 10, and 1.
Exercise 2: Create three overloaded formatTestResult methods: one that takes just a test name (defaults to PASS), one that takes name and status, and one that takes name, status, and duration in ms.
Exercise 3: Write a method reverseString(String s) without using StringBuilder.reverse(). Use a for loop to build the reversed string character by character.
Answer all 10 questions, then submit to see your score.
1. What is the output of: if (5 > 3) System.out.print("A"); System.out.print("B");
2. What happens if you omit break in a switch case?
3. How many times does this loop run? for (int i = 0; i < 5; i++)
4. What does the continue statement do in a loop?
5. What is the return type of a method that returns nothing?
6. Which loop runs the body at least once?
7. What is method overloading?
8. What is the scope of a variable declared inside a for loop?
9. What does int result = (10 > 5) ? 1 : 0; assign to result?
10. Is Java pass by value or pass by reference?