For example, there is a class Warrior which have linked class Sword. In class Sword defined field: public static int hp = 100; which shows the health points consumed by this type of weapon. There is need for a few classes Warrior. I think I need to define in class Warrior the link Sword (only once) to be able to get static field hp. How can I link it properly?
class public Warrior{
public String name;
public Sword s = new Sword(); // ???
}
class public Sword{
public static int hp = 100;
}
Will new Sword() create link to class each time new Warrior created?
Can I define Sword class as static inside another Weapon class? (There is a need for multiple classes like Sword)
Is following structure correct? Can outer class be static and hold inside another static?
class public Warrior{
public String name;
public int SwordHp = Weapon.Sword().hp;
public int BowHp = Weapon.Sword().hp;
}
(abstract?) public static class Weapon{
public static class Sword{
public static int hp = 100;
}
public static class Bow{
public static int hp = 90;
}
For getting Static field hp you do not need to create
In warrior class. Static variables initialize on load. So you can access you hp anywhere without declaring in the specific classes by just using
Sword.hp ;
This do not have any impact on static variables.
As i understand you Have a warrior and different weapons as currently Sword.Every weapon has health points hp and Every Warior has its own weapon and health points. If you use static fields then these health points will be shared among all warriors if 50 wariors then all will using just 100 points togather which i expect you do not want so you should use:
It will create a new sword with 100 health points every time a warrior is created and every warior will consume his own health points. Hope it will help.