DEV Community

Arshad Ali
Arshad Ali

Posted on

JAVA-Variable Scope

main.java



    public class Main {

        public static void main(String[] args) {

        //local = declared inside a method
        //        visible only to that method

        //global = declared outside a method, but within a class
        //         visible to all parts of a class

            DiceRoller diceRoller = new DiceRoller();
    }           

}
Enter fullscreen mode Exit fullscreen mode

class.java

import java.util.Random;

public class DiceRoller {

    Random random;
    int number = 0;


    DiceRoller(){
        Random random = new Random; 
        roll();
    }

    void roll() {
        number = Random.nextInt (6)+1;
        System.out.println(number);
    }

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)