ch02-4 - Scopeandclassconstants Chapter2.4 1...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Scope and class constants Chapter 2.4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Scope of a variable The part of a program where a given variable  exists . Its declaration to the end of the  {   }  braces in which it was declared. declared in a  for  loop:  exists only in that loop. declared in a method:  exists only to the end of the method. public static void example() { int x = 3; for (int i = 1; i <= 10; i++) { System.out.println(x); } // i no longer exists here } // x ceases to exist here x's scope      i's scope
Background image of page 2
3 Scope and using variables You can’t use a variable outside of its scope. public static void main(String[] args) { example(); System.out.println(x); // illegal for (int i = 1; i <= 10; i++) { int y = 5; System.out.println(y); } System.out.println(y); // illegal } public static void example() { int x = 3;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Overlapping scope These are legal – scopes do NOT overlap: public static void main(String[] args) { int x = 2; for (int i = 1; i <= 5; i++) { int y = 5; System.out.println(y); } for ( int i = 3 ; i <= 5; i++) { int y = 2 ; int x = 4; // illegal System.out.println(y); } } public static void anotherMethod() { int i = 6 ; int y = 3 ; System.out.println(i + ", " + y); }
Background image of page 4
5 Problem: redundant values
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Also called Magic Numbers Values that are used throughout the program. Normal variable won’t fix the magic number problem -- it will be out of scope. public static void main(String[] args) { int max = 3; printTop(); printBottom(); } public static void printTop() { for (int i = 1; i <= max ; i++) { // ERROR: max not found for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } public static void printBottom() { for (int i = max ; i >= 1; i--) { // ERROR: max not found for (int j = i; j >= 1; j--) { System.out.print( max ); // ERROR: max not found } System.out.println(); } }
Background image of page 6
7 Class constant example Making the 3 a class constant removes the redundancy:
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

ch02-4 - Scopeandclassconstants Chapter2.4 1...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online