为什么要使用 declare an object reference variable static final?

Why would you use declare an object reference variable static final?

我正在阅读 Java 《如何编程》第 10 版并浏览了前几章。对于这个例子,我们展示了如何确保 SecureRandom class 但有一部分让我完全困惑。

// Fig. 6.8: Craps.java
// Craps class simulates the dice game craps.
import java.security.SecureRandom;

public class Craps
{
    // create secure random number generator for use in method rollDice
    private static final SecureRandom randomNumbers = new SecureRandom();

    // enum type with constants that represent the game status
    private enum Status { CONTINUE, WON, LOST };

    // constants that represent common rolls of the dice
    private static final int SNAKE_EYES = 2;
    private static final int TREY = 3;
    private static final int SEVEN = 7;
    private static final int YO_LEVEN = 11;
    private static final int BOX_CARS = 12;

    // plays one game of craps
    public static void main(String[] args)
    {
        int myPoint = 0; // point if no win or loss on first roll
        Status gameStatus; // can contain CONTINUE, WON or LOST

        int sumOfDice = rollDice(); // first roll of the dice

        // determine game status and point based on first roll
        switch (sumOfDice)
        {
            case SEVEN: // win with 7 on first roll
            case YO_LEVEN: // win with 11 on first roll
                gameStatus = Status.WON;
                break;
            case SNAKE_EYES: // lose with 2 on first roll
            case TREY: // lose with 3 on first roll
            case BOX_CARS: // lose with 12 on first roll
                gameStatus = Status.LOST;
                break;
            default:
                gameStatus = Status.CONTINUE; // game is not over
                myPoint = sumOfDice; // remember the point
                System.out.printf("Point is %d%n", myPoint);
                break;
        }

        // while game is not complete
        while (gameStatus == Status.CONTINUE) // not WON or LOST
        {
            sumOfDice = rollDice(); // roll dice again

            // determine game status
            if (sumOfDice == myPoint) // win by making point
                gameStatus = Status.WON;
            else if (sumOfDice == SEVEN) // lose by rolling 7 before point
                gameStatus = Status.LOST;
        }

        // display won or lost message
        if (gameStatus == Status.WON)
            System.out.println("Player wins");
        else
            System.out.println("Player loses");
    }

    // roll dice, calculate sum and display results
    public static int rollDice()
    {
        // pick random die values
        int die1 = 1 + randomNumbers.nextInt(6); // first die roll
        int die2 = 1 + randomNumbers.nextInt(6); // second die roll

        int sum = die1 + die2; // sum of die values

        // display results of this roll
        System.out.printf("Player rolled %d + %d = %d%n", die1, die2, sum);

        return sum;
    }
} // end class Craps

书中提到它被声明为class、SecureRandom的private static final变量,这样一个对象总是被用来调用方法,rollDice().如果有一个程序包含 class Craps 的多个实例,它们将共享这个对象。我的问题是是否有可能需要 class SecureRandom 的多个实例?下一个问题是,既然是SecureRandom的对象引用变量,为什么还要调用Craps的变量?

Next question will be since this is an object referenced variable of SecureRandom, why is it still called as a variable of Craps?

您的变量 randomNumbersCraps class 的成员,类型为 SecureRandom。也就是说,它被定义为 Craps class 类型 SecureRandom 的变量。它被定义为静态意味着只有 randomNumbers 的一个副本并且它将被 Craps

的所有实例共享