Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Blocks, Scope
-
In Java, a block is a unit of code surrounded by curly braces {}.
-
We can create these a number of ways:
-
Loops: while, do while, for
-
Conditionals: if, switch
-
Functions
-
Inner classes
-
Syncronized blocks
-
Variables declared in that block have scope only in that block.
-
The NumberFormat objects I created in the promptUser() function
of the previous example can only be used in that function.
-
You cannot declare a variable in a block with the same name as a variable
outside that block. You will get a compiler error.
-
Exception: parameter values in functions can have the same name as class
attributes, or variables declared in the class. To access the parameter
value, simply use the variable name. To access the class attribute,
preface the name with this. This is actually fairly common.
Conditional Statements


Created by: Brandan
Jones January 4, 2002