Question

For my programming class in first year engineering I have to make a D-game in Java, with only very little knowledge of Java.

In one class I am generating a random integer via

public int rbug = (int)(Math.random() * 18);

every so many ticks. I have to use this integer in another class (in the requirements for an if-loop), and apparently it needs to be static. But when I change the variable to public int static, the value doesn't change any more.

Is there an easy way to solve this problem?


Edit: part of code added:

public int rbug = (int)(Math.random() * 18); 

which is used in

public void render(Graphics g){
  g.drawImage(bugs.get(rbug), (int)x, (int)y, null);

And in another class:

if(Physics.Collision(this,  game.eb, i, BadBug.rbug)){

}

As error for BadBug.rbug I get the message

Cannot make a static reference to a non-static field

Était-ce utile?

La solution

Using static to make things easier to access is not a very good ideal for design. You would want to make variables have a "getter" to access them from another class' instance, and possibly even a "setter". An example of this:

public class Test {

    String sample = 1337;

    public Test(int value) {
        this.sample = value;
    }

    public Test(){}

    public int getSample() {
        return this.sample;
    }

    public void setSample(int setter) {
        this.sample = setter;
    }

}

An example of how these are used:

Test example = new Test();
System.out.println(example.getSample()); // Prints: 1337
example = new Test(-1);
System.out.println(example.getSample()); // Prints: -1
example.setSample(12345);
System.out.println(example.getSample()); // Prints: 12345

Now you might be thinking "How do I get a string from the class that made the instance variable within the class?". That's simple as well, when you construct a class, you can pass a value of the class instance itself to the constructor of the class:

public class Project {

    private TestTwo example;

    public void onEnable() {
        this.example = new TestTwo(this);
        this.example.printFromProject();
    }

    public int getSample() {
        return 1337;
    }
}

public class TestTwo {

    private final Project project;

    public TestTwo(Project project) {
        this.project = project;
    }

    public void printFromProject() {
        System.out.println(this.project.getSample());
    }

}

This allows you to keep single instances of classes by passing around your main class instance.

To answer the question about the "static accessor", that can also be done like this:

public class Test {

    public static int someGlobal = /* default value */;

}

Which allows setting and getting values through Test.someGlobal. Note however that I would still say that this is a horrible practice.

Autres conseils

Do you want to get a new number every time that you want BadBug.rbug? Then convert it from a variable to a method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top