0

so I am making a game where the player's skill damage is determined by their Skill Level and their weapon Mastery. The two values are stored in an XML document, and I am using DOM to retrieve the values, and am trying to print their sum to the console.

public class Damage {

    public String skillName = "Bash"; //name of the skill

    Xml config = new Xml("C:/character.xml","config");//part of the XML retrieving
    Xml version = config.child("Character");//another part of the XML retrieving

    int mastery = version.integer("Mastery"); //mastery of the skill
    int skillLevel = version.integer("skillName");//skill level 
    int skillDamage = mastery + skillLevel; //adding the two values together

public static void main(String[] args) {    
    System.out.println(skillDamage);
}

}

When I run this code, it tells me that I can't have non-static variables in the static Main method. However, when I place the static tag before the int on the variables, it results in 0.

My question is: How can I make the variables static but still produce the sum of the two XML values? Could I somehow collect the non-static data from the XML, make it static, and then use that?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Jack
  • 380
  • 1
  • 3
  • 12

4 Answers4

2

Try

System.out.println(new Damage().skillDamage);

Because you need a instance for non-static class-variables

Grim
  • 1,938
  • 10
  • 56
  • 123
2

You need to create an instance of your Damage class first, if you want to use its non-static variables/members. Put your main method like this:

public static void main(String[] args) {    
    Damage dmg = new Damage();

    System.out.println(dmg.skillDamage);
}
Gee858eeG
  • 185
  • 8
2

I don't think you want the variables to be static.

1) Make skillDamage a public int

2) Then, just create your object in your main method:

Damage d = new Damage();
System.out.println(d.skillDamage);

It would probably be best to encapsulate skillDamage in a method, something like

public int getSkillDamage(){...}
mttdbrd
  • 1,791
  • 12
  • 17
  • 1
    +1 for inheritance-save getter. But if you use non-final getter: the field should be private/protected then. – Grim Mar 17 '14 at 00:23
1

Imagine that you have class cow. You can create instances of that class, for example berta and milka . That would mean, that you have two cows and their behaviour is based on class cow.

If you define something static it means, it is static to its class, therefore you can not define specific actions for each cow.

You should have a new class, for example "GameEngine", you should have all what you need there and you should create it with something like : GameEngine ge = new GameEngine(); and then use methods like ge.readXML();

libik
  • 22,239
  • 9
  • 44
  • 87