Skip to main content

Java Local Variable

Local Variable Rules in Java

  1. Local variables are declared in a blocks, methods or constructors
  2. Access modifiers cannot be used for declaring local variables.
  3. Stack memory is used for storing local variables and function calls and has a fixed size
  4. Local variables do not have default values. You must initialize them before using.
  5. Local variables declared in loops or blocks are destroyed after the block or loop completes

Rules for Local Variables in Java:

  1. Declaration and Initialization:

    • A local variable must be declared before it can be used.
    • Local variables must be initialized explicitly before use, as Java does not provide default values for them.
    java
    public void exampleMethod() { int num; // Declared but not initialized // System.out.println(num); // ERROR: Variable 'num' might not have been initialized num = 10; // Initialized System.out.println(num); // Works fine }
  2. Scope:

    • The scope of a local variable is limited to the block (method, constructor, or loop) in which it is declared.
    • The variable is destroyed when the block exits.
    java
    public void showScope() { if (true) { int x = 5; // Local variable System.out.println(x); // Accessible here } // System.out.println(x); // ERROR: x is out of scope }
  3. Access Modifiers:

    • Local variables cannot have access modifiers (e.g., public, private, protected).
    • They can only have final as a modifier to make them immutable.
    java
    public void test() { final int x = 10; // A final local variable // x = 20; // ERROR: Cannot assign a value to a final variable }
  4. No Default Value:

    • Unlike instance variables, local variables do not have default values. You must initialize them before using.
  5. Shadowing:

    • A local variable can shadow (hide) a class or instance variable with the same name. Use this to refer to the instance variable explicitly.
    java
    public class Example { int value = 20; public void method() { int value = 10; // Shadows the instance variable System.out.println(value); // Prints 10 System.out.println(this.value); // Prints 20 } }
  6. Loops and Blocks:

    • Local variables declared in loops or blocks are destroyed after the block or loop completes.
    java
    public void loopExample() { for (int i = 0; i < 5; i++) { System.out.println(i); // Accessible inside the loop } // System.out.println(i); // ERROR: i is out of scope }
  7. Lambda Expressions:

    • Local variables referenced in lambda expressions must be effectively final, meaning they cannot be reassigned after their initial assignment.
    java
    public void lambdaExample() { int num = 10; Runnable r = () -> System.out.println(num); // num is effectively final // num = 20; // ERROR: Variable used in lambda should be final or effectively final r.run(); }

Comments

Popular posts from this blog

Step-by-Step: Launch Browser, Context, and Page in Playwright and Run Test and Configuration (JavaScript)

🎥 Setup Browser, Context, Page & Run Config Test Scripts with package.json & playwright.config.js Step-by-Step: Launch Browser, Context, and Page in Playwright and Run Test and Configuration (JavaScript) 1. Install Playwright You can install Playwright using the following command: npm init playwright@latest 2. Create a Basic Test Script Understand the core Playwright architecture: Element Description browser Controls the browser instance (like Chrome, Firefox, etc.) context Acts like a separate browser profile (cookies, localStorage are isolated) page A single browser tab where interaction happens 3. Run the Test npx playwright test example.spec.js Ways to Run TypeScript Tests Way Command Notes 🟢 Via npx npx playwright test Uses built-in TypeScript support 🟢 With s...

How to Maximize a Browser window using Playwright Java

  Playwright itself does not have a direct maximize () method like some other browser automation tools, but you can achieve the effect by setting the viewport to the screen size of your display. Below are ways to handle Maximize a browser in Playwright Java  Ø   Setting the viewport to the screen size  o    setViewportSize(1920, 1080): This sets the browser window to a full HD resolution, effectively maximizing it. o    setHeadless(false): Ensures the browser runs in a visible mode so you can see the window being maximized.  Code Snapshot  Playwright playwright = Playwright. create ()   ;   Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));     // Create a new browser context   BrowserContext context = browser.newContext(new Browser.NewContextOptions()          .setViewportSize(1920, 1080));     ...

Playwright Test Structure in Details -Session-02

🎥 Playwright: test.only, Hooks & Grouping with test.describe Explained Let’s go step-by-step , showing how to build from a single test , to using beforeEach / afterEach , and then organizing things with test.describe . ✅ Step 1: Basic Single Test with test.only import { test, expect } from '@playwright/test' ; test. only ( '🚀 Basic test - check title' , async ({ page }) => { await page. goto ( 'https://example.com' ); await expect (page). toHaveTitle ( /Example Domain/ ); }); test.only ensures only this test runs — great for debugging. ✅ Step 2: Add beforeEach and afterEach import { test, expect } from '@playwright/test' ; test. beforeEach ( async ({ page }) => { console . log ( '🔄 Setting up before each test' ); await page. goto ( 'https://example.com' ); }); test. afterEach ( async ({ page }, testInfo) => { console . log ( `📦 Finished test: ${testInfo.title} `); }); test. only ( ...