Search code examples
javaxmlvariablesstaticmain-method

Using XML-derived Variables in the Main Method


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?


Solution

  • Try

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

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