Stan
Stan

Reputation: 3699

Accesing a int value from a method into another method?

I have a method, which generates a random number, heres the code for it:

    public int generateNumber(){
    int randomnum = generator.nextInt(5);
    System.out.println(randomnum);
    return randomnum;
}

Now, I want to acces the randomnum int value, into another method, in the same class file, in an if-statement. Like this:

public DrawPanel(){  
    timer.schedule(new LeTimer(), 0, 1*1000);

    if(randomnum == 3){
         System.out.println("3 has been counted.");
    }
}

How would I do this?

Upvotes: 0

Views: 195

Answers (1)

nanobar
nanobar

Reputation: 66375

declare the int in class scope then all methods can access it

class
{
    <class scope variables>

    method1() { }
    method2() { }
}

or simply call the method directly inside the other method

if(generateNumber() == 3){
     System.out.println("3 has been counted.");
}

Upvotes: 3

Related Questions