Nicola Daaboul
Nicola Daaboul

Reputation: 77

Why can't I an array in a method outside its class?

I have created the array and outside of its class I have created a method to sort the array. It keeps saying it can't find the variable name of the array I made. When I take the method and put it into the same class as the array it works but it defeats the purpose of what I'm trying to achieve, help?

/**
 * @param args the command line arguments
 */
    public static void main(String[] args) {
        // TODO code application logic here

        System.out.println("Enter a length for the array: ");
        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();

        int randomNumbers[] = new int[x];

        for (int index = 0; index < randomNumbers.length; index++)

        {
            randomNumbers[index] = (int) (Math.random() * 100);
        }

        for (int index = 0; index < randomNumbers.length; index++)

        {
            System.out.println(randomNumbers[index]);
        }

    }

    static void sortAscending()

    {
        Arrays.sort(randomNumbers);

        for (int i = 1; i < randomNumbers.length; i++) {
            System.out.println("Number: " + randomNumbers[i]);
        }
    }  

Upvotes: 0

Views: 1134

Answers (1)

fabian
fabian

Reputation: 82461

Since randomNumbers is declared in the main method, other methods can't access it. There are several ways to make the array accessible from the other method, e.g.:

  1. pass as parameter to the method:

    static void sortAscending(int[] randomNumbers) {
        //...
    }
    

    and call sortAscending call from main like this

    sortAscending(randomNumbers);
    
  2. Pass the value through a field. I wouldn't use a static field however, since there's only one of these fields for all instances. But you could make use a instance of your class and store the value in a non-static field:

    publc class MyClass {
    
        // declare randomNumbers as field
        private int[] randomNumbers;
    
        public static void main(String[] args) {
            MyClass o = new MyClass();
            o.localMain(args);
    
            // you could call sortAscending here like this
            o.sortAscending();
        }
    
        // you don't really need to pass args, since you don't use it
        public void localMain(String[] args) {
            // TODO code application logic here
    
            System.out.println("Enter a length for the array: ");
            Scanner scan = new Scanner(System.in);
            int x = scan.nextInt();
    
            // assing new value to field
            randomNumbers = new int[x];
    
            for (int index = 0; index < randomNumbers.length; index++)
            {
                randomNumbers[index] = (int) (Math.random() * 100);
            }
    
            for (int index = 0; index < randomNumbers.length; index++)
            {
                System.out.println(randomNumbers[index]);
            }
    
        }
    
        void sortAscending()
        {
            Arrays.sort(randomNumbers);
    
            for (int i = 1; i < randomNumbers.length; i++) {
                System.out.println("Number: " + randomNumbers[i]);
            }
        }  
    
    }
    

Upvotes: 2

Related Questions