AP® Computer Science A glossary
Local variable
Course: AP® Computer Science A Shows up in: Unit 3: Class Creation
A variable declared in the header or body of a block of code, including a method or constructor parameter, usable only in that block.
Appears in the course and exam description (CED): 3.8.A.13.8.A.2
Local variables cannot be declared public or private. A local variable or parameter with the same name as an instance variable hides it inside that method or constructor.
public class Rain
{
public static int total(int days) // days is local
{
int sum = 0; // sum is local
for (int d = 1; d <= days; d++) // d is local to the loop
{
sum += d;
}
return sum;
}
public static void main(String[] args)
{
System.out.println(total(4));
}
}
Output:
10 Learn it properly, with a checkpoint 3.8 Scope and Access