How to access an array from another class

Blnpwr

I want to operate on the array called "players" that is declared in the main method. I want to use "players" in my class called "Glucksspielthread" I know that I can't access "players" because it is declared in the main method and is not visible for other classes.

How can I solve this problem? Here is my code:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Glucksspieltest {
    public static void main(String[] args) {
        int numPlayers = Integer.parseInt(args[0]);
        int threadSize = Integer.parseInt(args[1]);

        ExecutorService es = Executors.newFixedThreadPool(threadSize);
        Glucksspielthread[] players = new Glucksspielthread[numPlayers];
        for (int i = 0; i < numPlayers; i++) {
            players[i] = new Glucksspielthread(i);
            es.execute(players[i]);
        }
    }
}

class Thinker {
    public static void think(int Millisekunden) {
        try {
            Thread.sleep(Millisekunden);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void randomThink(int minMillisekunden, int      maxMillisekunden) {
        System.out.println("test");
    }
}

class Glucksspielthread implements Runnable {
    public int playerNumber;

    Glucksspielthread(int number) {
            playerNumber = number;
    }

    @Override
    public void run() {
        for (int i = 0; i <= playerNumber; i++) {
            // here, I want to operate on array called "players" that is   declared in the main method  
        }
    }
}
dumitru

Just for your test purpose make your players variable static and public in the Glucksspieltest class, like this:

public class Glucksspieltest {

public static Glucksspielthread[] players;

Then acces it in the Glucksspielthread class like this:

for (int i = 0; i <= playerNumber; i++) {
        // here, I want to operate on array called "players" that is   declared in the main method
        Glucksspieltest.players
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access an array from another class

From Dev

How to Access textarea from another class?

From Dev

How to access an object from another class

From Dev

How to access DefaultTableModel from another class

From Dev

How to access an ArrayList from another class

From Dev

How to access ArrayList from another class

From Dev

how to access variable from another class in python

From Dev

How to access SurfaceView from another class?

From Dev

How to access a method from a class from another class?

From Dev

objective-c : How can I access variables or array from another class?

From Dev

How to edit an array from another class in Java

From Dev

How to get an array from another class

From Dev

How to get an array from another class

From Dev

how to modify an array from another class

From Dev

Java access array from inside another class method

From Dev

How to access to widget class from another dialog class

From Dev

C# How to access static class List<> from another class

From Dev

How to access to widget class from another dialog class

From Dev

How to allow only one class to access a method from another class?

From Dev

How to properly access a StringVar() of a class from another class - Python - tkinter

From Dev

How to place arrays from a class into a single array located in another class?

From Dev

How to make an array from a class defined inside another class

From Dev

Access returnValue from another class

From Dev

Access listbox from another class?

From Dev

Access Window from another Class

From Dev

Access NSTextBox from another class

From Dev

Access listbox from another class?

From Dev

Access data from another class

From Dev

How do I access an enum from another class in Swift?

Related Related

  1. 1

    How to access an array from another class

  2. 2

    How to Access textarea from another class?

  3. 3

    How to access an object from another class

  4. 4

    How to access DefaultTableModel from another class

  5. 5

    How to access an ArrayList from another class

  6. 6

    How to access ArrayList from another class

  7. 7

    how to access variable from another class in python

  8. 8

    How to access SurfaceView from another class?

  9. 9

    How to access a method from a class from another class?

  10. 10

    objective-c : How can I access variables or array from another class?

  11. 11

    How to edit an array from another class in Java

  12. 12

    How to get an array from another class

  13. 13

    How to get an array from another class

  14. 14

    how to modify an array from another class

  15. 15

    Java access array from inside another class method

  16. 16

    How to access to widget class from another dialog class

  17. 17

    C# How to access static class List<> from another class

  18. 18

    How to access to widget class from another dialog class

  19. 19

    How to allow only one class to access a method from another class?

  20. 20

    How to properly access a StringVar() of a class from another class - Python - tkinter

  21. 21

    How to place arrays from a class into a single array located in another class?

  22. 22

    How to make an array from a class defined inside another class

  23. 23

    Access returnValue from another class

  24. 24

    Access listbox from another class?

  25. 25

    Access Window from another Class

  26. 26

    Access NSTextBox from another class

  27. 27

    Access listbox from another class?

  28. 28

    Access data from another class

  29. 29

    How do I access an enum from another class in Swift?

HotTag

Archive