Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit static field and change it's value?

I'm working on program/game where I have static utility class with params.

class ParamsGeneral {
   public static final int H_FACTOR = 100;
   public static int MAX_SCORE = 1000;
   ...
}

then I need to override this values in some specific cases, for example playing on map with limited score. So I did following:

class ParamsLimited extends ParamsGeneral {
   public static int MAX_SCORE = 500;
   // other params stay same
}

And the intended usage is following:

class Player {
   ParamsGeneral par;
   public Player() {
      if(onLimitedMap()){
          par = new ParamLimited();
      }
   }

   public boolean isWinner() {
      if(this.score == par.MAX_SCORE) {
          return true;
      }
      return false;
   }
}

I haven't actually tested this code, because IDE is complaining about calling static field through instance and also about field hiding. I clearly see that this code is stinks, so is there a way to achieve this or do I have to write each param class separately?

PS: I know I shoud make the default class abstract and use getters, I'm just curious if there is a way to make the values accesible statically.

like image 210
jnovacho Avatar asked Apr 24 '13 21:04

jnovacho


People also ask

Can value of static variable be changed?

Static methods cannot access or change the values of instance variables or the this reference (since there is no calling object for them), and static methods cannot call non-static methods.

Can static fields be inherited?

Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor. However, they can contain a static constructor.

Can a static field change?

The short answer to the topic question is no. You can't access non-static fields and methods from a static context in Java. You can do only oposite: reach statics from object instance.

Can a static field be changed in Java?

A static variable is common for all instances of the class. A final variable can not change after it has been set the first time. So a static final variable in Java is common for all instances of the class, and it can not be changed after it has been set the first time.


Video Answer


2 Answers

You cannot override static members - in Java, neither methods nor fields could be overriden. However, in this case it does not look like you need to do any of that: since you have an instance of ParamsGeneral in the par variable, a non-static method would do what you need with the regular override.

class ParamsGeneral {
    public int getMaxScore() {
        return 1000;
    }
}
class ParamsLimited extends ParamsGeneral {
    @Override public int getMaxScore() {
        return 500;
    }
}

...

public boolean isWinner() {
    // You do not need an "if" statement, because
    // the == operator already gives you a boolean:
    return this.score == par.getMaxScore();
}
like image 67
Sergey Kalinichenko Avatar answered Nov 15 '22 15:11

Sergey Kalinichenko


I wouldn't use subclassing for a general game vs a limited game. I would use an enumeration, like:

public enum Scores {
    GENERAL (1000),
    LIMITED (500),
    UNLIMITED (Integer.MAX_INT);

    private int score;
    private Scores(int score) { this.score = score; }
    public int getScore() { return score; }
}

Then, when constructing a game, you can do:

Params generalParams = new Params(Scores.GENERAL);
Params limitedParams = new Params(Scores.LIMITED);

And so forth.

Doing it this way allows you to change the nature of your game while keeping your values centralized. Imagine if for every type of parameter you think of you have to create a new class. It could get very complicated, you could have hundreds of classes!

like image 24
durron597 Avatar answered Nov 15 '22 15:11

durron597