Java Lesson 4
Java Lesson 4
https://blogs.ashrithgn.com
Objectives
• provides a meaningful description of what the values stand for, for example,
number = UNDEFINED; is more meaningful than number = -1
• provides easier program maintenance
• only need to change the value in the constant declaration instead of locating all
occurrences of the same value in the program code
import java.util.Random;
class Dice {
private static final int MAX_NUMBER = 6;
private static final int MIN_NUMBER = 1;
private static final int NO_NUMBER = 0;
private int number;
private Random random;
public Dice( ) {
random = new Random();
number = NO_NUMBER;
}
Faculty of Computer Science
Cond
• Local variables are declared within a method declaration and used for temporary
services, such as storing intermediate computation results.
return result;
}
• how to use constants, a meaningful description of what the values stand for