Java Scope

JK JK 2038 
Created at
Updated at  
7,201 0 0

In Java, variables are only accessible inside the region they are created. This is called scope.


Method Scope

Variables declared directly inside a method are available anywhere in the method following the line of code in which they were declared:

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    int x = 100;

    // Code here can use x
    System.out.println(x);
  }
}

Block Scope

A block of code refers to all of the code between curly braces {}.

Variables declared inside blocks of code are only accessible by the code between the curly braces, which follows the line in which the variable was declared:

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

    } // The block ends here

  // Code here CANNOT use x

  }
}

 

Tags Block Scope Java Java Scope Method Scope Facebook X
Comments 0
Similar posts
  1. Java Recursion
    7,270
  2. Java Methods
    7,177
  3. Java Classes and Objects
    7,205
  4. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    8,048
  5. Java Switch Statements
    7,513
  6. Java Short Hand If...Else (Ternary Operator)
    7,686
  7. Java If ... Else
    7,508
  8. Java Packages
    7,259
  9. Java Inheritance (Subclass and Superclass)
    7,256
  10. Java Math
    7,536
  11. Java Variables
    7,585
  12. Java Comments
    8,210
  13. Java Polymorphism
    7,139
  14. The Print() Method
    7,237
  15. Java Syntax
    7,558
  16. Java Getting Started
    7,506
  17. Java Inner Classes
    7,162
  18. What is Java?
    7,164
  19. Machine Learning Types and Programming Languages
    7,344
  20. Java Tutorials associated with AP Computer Science A
    7,775
  21. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,274
  22. Java Servlet Example
    7,199
  23. Difference between Java and Javascript
    7,748
  24. Challenge: One Code Problem Per Day
    1,278
  1. How do I replace content that based on the HTML UI Template
    7,236
  2. Dataset of California Foodbanks
    7,250
  3. Java Abstract Classes and Methods
    7,902
  4. Java Arrays
    9,052
Recently updated
  1. Comprehensive Guide to Recent Critical PHP Vulnerabilities
    899
  2. How to Check Your UIUC Graduation Progress Using DARS Audit
    797
  3. Michael Jackson's Billie Jean
    390
  4. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    346
  5. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    321
  6. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    351
  7. Watch This Dazzling Dancer’s Mind-Blowing Moves
    366
  8. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    378
  9. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    1,274
  10. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    535
  11. The Evolution and Production Reality of Agentic AI
    415
  12. How to Activate or Waive Your UIUC Student Health Insurance
    527
  13. Complete Guide to Building a Machine Learning Model
    540
  14. My life cuts at Las Vegas during Thanksgiving day holiday
    7,601
  15. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    572
  16. Harness vs. OpenClaw: Two Very Different "Agents"
    1,340
  17. Clean Python Environments: The Power of venv vs. Docker
    1,091
  18. What is Docker? Why is Docker also useful in a development environment?
    806
  19. UIUC 2026-2027 Academic Calendar
    2,005
  20. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    1,064
  21. Open-Source LLMs: The AI Revolution
    970
  22. Resume 2.0: Leveling Up for My First Software Gig
    2,962
  23. Not everyone will understand what this man just did
    2,434
  24. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,850
  25. Unpacking IU's Shopper
    972
  26. Jackie Chan's Police Story: The Action Masterpiece
    854
  27. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,191
  28. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,378
  29. Open Databases for Sex Crime Occurrences in the U.S.
    962
  30. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    3,397
  31. My First Day at University of Illinois-Urvana Champaign
    1,370
  32. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,409
  33. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,479
  34. Sipping the Stars: My Starbucks Adventure
    10,140
  35. Exciting explore at Sequoia National Park
    7,852
  36. My Life Shot at Death Valley
    2,050
  37. Ip Man fights with Muay Thai Master
    1,127
  38. Mad Clown - Don't Die
    1,242
  39. How to get Student Enrollment and Degree Verification at UIUC
    5,846
  40. LAX Thanksgiving Rush: A Joyful Reunion
    1,079
  41. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,350
  42. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,145
  43. 10CM - To Reach You (너에게 닿기를)
    1,326
  44. Feeling weak? Transform yourself at the UIUC ARC!
    1,735
  45. BOYNEXTDOOR - If I Say I Love You
    1,372
  46. The Future of Software Engineer - AI Engineering
    1,093
  47. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    1,105
  48. Lie - Legend song by BIGBANG
    7,995
  49. Why ROLLBACK is useful when you work with Google Gemini CLI?
    1,096
  50. Reimbursement after Vaccination at McKinley Health Center
    1,212