[Solved] How would I access an int variable from one class to another? (Java)


Before trying to work with Bukkit I’d recommend you to get some Java experience first. That’s not meant as an insult, but it can get quite confusing if you do it the other way round. Anyways, if you still want to know the answer to your question:

You’ll have to create a getter & setter for your “killcount” variable.

class Xyz {

    private int killcount;

    public void setKillcount(int killcount) {
        this.killcount = killcount;
    }

    public int getKillcount() {
        return this.killcount;
    }

}

Of course this is a simplified version without checks, but if you want to access the variable from a different class you can create an instance and use the methods to modify it.

public void someMethod() {

    Xyz instance = new Xyz();
    instance.setKillcount(instance.getKillcount() + 1);
    //this would increase the current killcount by one.

}

Keep in mind that you’ll have to use the same instance of the class if you want to keep your values, as creating a new one will reset them to default. Therefore, you might want to define it as a private variable too.

solved How would I access an int variable from one class to another? (Java)