Skip to main content
deleted 7 characters in body
Source Link
Caleb
  • 111
  • 2

Since the Arena class has no state, and what state it needs is passed in as arguments, it seems reasonable to me for the methods to be static. This is a common pattern for utility-like classes. To make it more explicit you can add the keyword final to the class to indicate that no-one should create an instance of this class (there would be no point).

However the way you're accessing that method is not very standard. If you want it to be static you should really be accessing it like so:

Arena.battle(mike, tom);

If, as others have suggested, you want Arena to be a class from which instance objects are created, rather than a utility class, then I'd suggest giving it some state. Your battle method already takes two Hero instances. An alternative approach would be to set the Hero instances on your Arena object and then call battle like so:

class Arena {

    private Hero h1;
    private Hero h2;

    public static void battle() {
        h1.setHp(h1.getHp()-h2.getPhysical_attack());
        h1.info();
    }

    public static void main(String[] args) {
        System.out.println("Entering Arena ...");

        Hero mike = new Hero();
        mike.setUname("mike");
        mike.info();

        Hero tom = new Hero();
        tom.setUname("tom");
        tom.info();

        Arena arena = new Arena();
        arena.hero1 = mike;
        arena.hero2 = tom;
        arena.battle();
    }
}

That way you can call other methods on the Arena in the future and not have to remember which Heros you passed to it. For example, a method that prints the result of the battle, or an average result of multiple battles over time.

Since the Arena class has no state, and what state it needs is passed in as arguments, it seems reasonable to me for the methods to be static. This is a common pattern for utility-like classes. To make it more explicit you can add the keyword final to the class to indicate that no-one should create an instance of this class (there would be no point).

However the way you're accessing that method is not very standard. If you want it to be static you should really be accessing it like so:

Arena.battle(mike, tom);

If, as others have suggested, you want Arena to be a class from which instance objects are created, rather than a utility class, then I'd suggest giving it some state. Your battle method already takes two Hero instances. An alternative approach would be to set the Hero instances on your Arena object and then call battle like so:

class Arena {

    private Hero h1;
    private Hero h2;

    public static void battle() {
        h1.setHp(h1.getHp()-h2.getPhysical_attack());
        h1.info();
    }

    public static void main(String[] args) {
        System.out.println("Entering Arena ...");

        Hero mike = new Hero();
        mike.setUname("mike");
        mike.info();

        Hero tom = new Hero();
        tom.setUname("tom");
        tom.info();

        Arena arena = new Arena();
        arena.hero1 = mike;
        arena.hero2 = tom;
        arena.battle();
    }
}

That way you can call other methods on the Arena in the future and not have to remember which Heros you passed to it. For example, a method that prints the result of the battle, or an average result of multiple battles over time.

Since the Arena class has no state, and what state it needs is passed in as arguments, it seems reasonable to me for the methods to be static. This is a common pattern for utility-like classes. To make it more explicit you can add the keyword final to the class to indicate that no-one should create an instance of this class (there would be no point).

However the way you're accessing that method is not very standard. If you want it to be static you should really be accessing it like so:

Arena.battle(mike, tom);

If, as others have suggested, you want Arena to be a class from which instance objects are created, rather than a utility class, then I'd suggest giving it some state. Your battle method already takes two Hero instances. An alternative approach would be to set the Hero instances on your Arena object and then call battle like so:

class Arena {

    private Hero h1;
    private Hero h2;

    public void battle() {
        h1.setHp(h1.getHp()-h2.getPhysical_attack());
        h1.info();
    }

    public static void main(String[] args) {
        System.out.println("Entering Arena ...");

        Hero mike = new Hero();
        mike.setUname("mike");
        mike.info();

        Hero tom = new Hero();
        tom.setUname("tom");
        tom.info();

        Arena arena = new Arena();
        arena.hero1 = mike;
        arena.hero2 = tom;
        arena.battle();
    }
}

That way you can call other methods on the Arena in the future and not have to remember which Heros you passed to it. For example, a method that prints the result of the battle, or an average result of multiple battles over time.

Source Link
Caleb
  • 111
  • 2

Since the Arena class has no state, and what state it needs is passed in as arguments, it seems reasonable to me for the methods to be static. This is a common pattern for utility-like classes. To make it more explicit you can add the keyword final to the class to indicate that no-one should create an instance of this class (there would be no point).

However the way you're accessing that method is not very standard. If you want it to be static you should really be accessing it like so:

Arena.battle(mike, tom);

If, as others have suggested, you want Arena to be a class from which instance objects are created, rather than a utility class, then I'd suggest giving it some state. Your battle method already takes two Hero instances. An alternative approach would be to set the Hero instances on your Arena object and then call battle like so:

class Arena {

    private Hero h1;
    private Hero h2;

    public static void battle() {
        h1.setHp(h1.getHp()-h2.getPhysical_attack());
        h1.info();
    }

    public static void main(String[] args) {
        System.out.println("Entering Arena ...");

        Hero mike = new Hero();
        mike.setUname("mike");
        mike.info();

        Hero tom = new Hero();
        tom.setUname("tom");
        tom.info();

        Arena arena = new Arena();
        arena.hero1 = mike;
        arena.hero2 = tom;
        arena.battle();
    }
}

That way you can call other methods on the Arena in the future and not have to remember which Heros you passed to it. For example, a method that prints the result of the battle, or an average result of multiple battles over time.