Skip to main content
United StatesComputer ScienceSyllabus dot point

What is the scope of a local variable, and how do public and private control access to members?

Topic 5.8 Scope and Access: distinguish local variable scope from instance variable scope, and use the access modifiers public and private to control where a class member can be used.

A focused answer to AP CSA Topic 5.8, covering the scope of local variables and parameters versus instance variables, variable shadowing, the public and private access modifiers, and how access control supports encapsulation, with a fully worked example.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Local variable scope
  3. Instance variable scope
  4. Shadowing
  5. Access modifiers: public and private
  6. Try this

What this topic is asking

The College Board (Topic 5.8) wants you to understand scope - where a variable can be used - and access, controlled by the modifiers public and private. Local variables and parameters are visible only inside the block or method where they are declared, while instance variables are visible throughout the object. Access modifiers determine which parts of a program may use a member, and they are the mechanism behind encapsulation.

Local variable scope

public void m() {
  int a = 1;             // in scope for the whole method
  for (int i = 0; i < 3; i++) {
    int b = i * 2;       // b and i are in scope only inside the loop
  }
  // here a is in scope; b and i are NOT
}

A for loop's control variable, declared in its header, is also limited to the loop.

Instance variable scope

An instance variable, declared in the class body, is in scope in every method of the object. That is what lets accessors and mutators in different methods all read and write the same private field.

Shadowing

This is why setter parameters are sometimes named differently from the field, or qualified with this, to avoid accidentally assigning a parameter to itself.

Access modifiers: public and private

A private instance variable is invisible to code in other classes; they must go through public accessors and mutators. This lets the class validate changes and hide its internal representation.

Try this

Q1. A variable is declared inside a for loop body. Where is it in scope? [1 point]

  • Cue. Only inside that loop body (the braces); it does not exist before or after the loop.

Q2. Explain the difference between public and private access, and why instance variables are usually private. [2 points]

  • Cue. public members are accessible from any class; private members only within their own class. Instance variables are private so outside code cannot change them directly, letting the class control and validate its data (encapsulation).

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. Consider the following method. ```java public void demo() { int x = 5; if (x > 0) { int y = 10; x = x + y; } System.out.println(x + y); } ``` What happens? (A) It does not compile, because y is out of scope at the println. (B) It prints `15`. (C) It prints `25`. (D) It prints `5`. (E) It prints `10`.
Show worked answer →

The answer is (A).

y is declared inside the if block, so its scope is limited to that block - it does not exist after the closing brace. The println is outside the if, so y is out of scope there, and the reference x + y fails to compile. (B) and (C) assume y is still visible. The fix would be to declare y before the if if it is needed afterward.

Markers reward knowing that a variable declared inside a block is only in scope within that block, including the braces of an if.

AP 2020 (style)3 marksFree response (code writing). A class `Locker` has a private int instance variable `code`. Write a public accessor `getCode` that returns `code`, and explain in a one-line comment why making `code` private rather than public improves the design.
Show worked answer →

A 3-point question testing access control and encapsulation.

// code is private so it can only be read or changed through this class's
// methods, which lets the class control and protect its data (encapsulation).
public int getCode() {
  return code;
}

Point 1: a public accessor with the correct return type. Point 2: it returns the private instance variable. Point 3: the comment explains that private restricts direct access to the class's own methods, supporting encapsulation. Stating that private only affects speed, or that it makes the variable shared, would be wrong.

Related dot points

Sources & how we know this