CS 2110 Lecture Notes - Lecture 5: Local Variable

24 views5 pages

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;

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents