Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This method will work, everything is scoped correctly. */
- public static void countToTen() {
- int count = 1; //count is declared outside the while loop;
- //the while loop can access the variable.
- while ( count <= 10 ) {
- System.out.print( count + " " );
- count = count + 1;
- }
- }
- /* This method will not compile. The variable nCubed is defined with too narrow a
- scope for the return statement to access it. It's declared inside the 'if'
- statement's braces, and the return is outside the 'if' statement's braces.
- */
- public static int cubed( int n ) {
- if ( n < 0 ) {
- int nCubed = n * n * n * -1;
- } else {
- int nCubed = n * n * n;
- }
- return nCubed; //compile error here
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement