Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am working on a program and I want these variables and strings, to be called on another class. So I have these:

    public static boolean newgame = false;
 * Declares Character Status
public boolean male = false;
public boolean female = false;
public static String charactername;
public static String characterclass;
public static int hp = 100;
public static int mp = 100;
public static int charstr = 5;
public static int chardef = 5;
public static int charagi = 5;
public static int chararank;
public static int charaprofile;
public static double criticalhit = 0.0;
public static double luck = 0.0;

 * Determines how each classes time fighting.
public static int timeFightSlow[] = {4000, 4500, 4800, 5000};
public static int timeFightMedium[] = {2000, 2500, 2700, 2860,3000};
public static int timeFightQuick[] = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};

 * Declares Items:
public static String gender;
 * Declares Classes

public static String sf = "Swordsman";
public static String sm = "Sword Mystic";
public static String warrior = "Warrior";
public static String duelist = "Duelist";
public static String lancer = "Lancer";

public static int experience = 0;
public static int iniexp = 100;
public static double totalexp = iniexp*2.5;
public static int initialhp = 100;
public static int hptotal = initialhp+10;
public static int initialmp = 50;
public static int mptotal = initialmp*2;

The problem I get is how can I call them up on a new class? Can I call them again or I should make another integer for those?

Asking a question like this (including the "variables and strings" part -- these are different things) suggests you need to learn the absolute basics of general programming concepts, Java and OO. Please do yourself a favour and read a book explaining the basics first or follow a bunch of tutorials. In this particular case "public static" all over the place means you're heading towards a wall if you continue this way. In general public fields (and public static in particular) should never be used unless you're doing final-round micro-optimisations, e.g. on a mobile platform. –  Gilead Feb 27 '12 at 20:00

3 Answers 3

You want to be able to use these variables from another class? If you want them to be different in the class (eg you can change them in one, but it wouldn't affect another class accessing the variables) then do what skyjlv said.

If you mean you just want to be able to access them from another class then put them in an interface and either implement the interface on the class (So you don't have to do Interface.variable) or just use them directly from the other class by going Interface.variable (This is the more advisable approach)

Like this

public interface Constants {
  public int blah = 1;
  public String foo = "bar";

public class bazz implements Constants {

     public bazz() {
        System.out.println(blah); //Prints 1


public class bazz {
     private int blah = Constants.blah;       

     public bazz() {
        System.out.println(blah); //Prints 1
wait it says here that i should remove the final modifier...I am working on a java class named Sairou.java and Character.java, i placed the strings and variables on Character and I want to call them in Sairou class but that error is appearing. –  Elven X Feb 26 '12 at 11:09
public final class Constants
  public static boolean male = false;
  public static boolean female = true;


if (this.isFemale == Constants.female)

You probably want to have a look at enums instead for flags though, they are more flexible and powerful.

I know its popular to use interfaces/base classes to inherit constants, but unless your class is supposed to actually BE a constant, it should never inherit or implement a Constants class/interface. Composition is much more preferred than inheritance in cases like this, you only spend a second typing that extra "Constants." but if you decide to inherit to save those keystrokes you spend the rest of the programs lifetime with an inheritance that shouldn't really be there in the first place.

is the this method applicable both to all the booleans? –  Elven X Feb 26 '12 at 10:59
@ElvenX Yes it is –  Daniel Carlsson Feb 28 '12 at 23:47

You could create an abstract class which contains all the variables. And then have this class and another class that you're planning to have similar variables both extend the abstract class.


Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.