jeudi 28 janvier 2021

How to access a random generated integer from other class without generating other random number

I have two classes

Class 1

public class Random {

    public static int random() {
        Random random = new Random();
        return random.nextInt(10);
    }
    public static int number = random();
}

and Class 2

public class FirstClass {
    
    static int generatedNumber = Random.number;

    public static void main(String[] args) {

            System.out.println(generatedNumber);
    }
}

Every time I try to access the generatedNumber variable of Class 2 from a third Class, the number I get is different and not the one received in Class 2.

My question is how I can make the number generated in Class 2 persist to be able to call it from other Classes and that it is always the same number generated in Class 2.

Thank you!




Aucun commentaire:

Enregistrer un commentaire