Back to Writing Classes

Lesson PreviewLesson 8 of 10 in this unit

5.8 Scope and Access

Part of Writing Classes in AP® Computer Science A. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

Scope refers to where a variable is accessible in a program. Think of scope as a container or "bubble" around a variable that determines where you can use that variable in your code. A variable's scope is defined by the location where it is declared. There are two primary types of scope in Java: local scope and global scope.

When a variable is declared inside a method, constructor, or block (e.g., a loop or conditional), it is said to have local scope. This means the variable only exists while that block of code is being executed, and it cannot be accessed from outside that block.

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science A.