CS 2110 Lecture Notes - Lecture 5: Local Variable
Document Summary
Local variables belong inside some block of code. Like in an if statement or method. Suppose in some block of code we want to swap the values of b and c. Basically, because we declared temp inside the block of code of the if statement, it only exists inside that block of code. If you tried to write temp = 5 outside that block, and it was not declared outside the if block, then you get an error. After that if statement (the one above), we know b <= c, so we can assert that after to be sure our code works. Scope is the area of code that certain variable/field can be used in. The innermost declaration of a variable is the one the code can refer to. Public class scopequiz { private int a; public scopequiz(int b) { System. out. println(a); int a = b + 1; this. a = a;