Variables Flashcards
What is a variable?
A name that labels a value. It gives us a way to remember what is being stored.
What is a variable’s scope? Discuss the three main types.
Scope defines where a certain variable or method is accessible in a program. Variables can be defined as having one of three types of scope:
1) Class level scope (instance variables): any variable declared within a class is accessible by all methods in that class. Depending on its access modifier (ie. public or private), it can sometimes be accessed outside the class.
2) Method level scope (local variables): any variable declared within a method, arguments included, is NOT accessible outside that method.
3) 3) Block scope (loop variables): any variable declared in a for loop condition is not accessible after the loop, unless you defined it beforehand.
What is a literal? Give examples.
A literal is “any notation for representing a value within source code”
What happens if you define a variable without assigning a value, and then use the variable?
Error