Access sub classes static final variable from superclass

Hi everyone,

I’m currently working on a project in which I have different types of “bullets”. I have a superclass called Projectile to handle all the needed functions that are common to all types of bullets. In fact, the only thing that can change from one bullet type to another are some variables : damage and pierce.

Because I will spawn a lot of each bullet type, I created one subclass for each bullet type, in where I had in mind to define damage and pierce as final static variables so that they would only be stored once in memory for each bullet type (I think this is how it works with static var).

So this is what I got for now :

class Projectile{
  int damage, pierce;  //if I don't define them here, I can't access them with superclass functions

  Projectile(Tower fired_from_tower, float x_dep, float y_dep, float direction){
    //some stuff

  //some functions

class Dart extends Projectile{ //this is the entire subclass and other bullet types are equivalent
  static final int damage = 1, pierce = 1;
  Dart(Tower fired_from_tower, float x_dep, float y_dep, float direction){
    super(fired_from_tower, x_dep, y_dep, direction);

The problem is, if I remove int damage, pierce; in the superclass, the superclass functions can’t recognize them, but if I do, and try to access these variables in the superclass, they’re value are still 0 (the value only changed in the subclasses).

My question is: is there a way to get the subclasses values from the superclass? Also, I just have this subclasses struc to save a bit of memory but since my whole project isn’t very optimised, if that wouldn’t save much (I would at most spawn 500 of each bullet type), I could just stay with the superclass Projectile and a set_damage_and_pierce() function (but I would loose the static final adventage)


1 Like

There is no need to store them as static final class members, simply use the ones in the superclass.


No - the superclass never knows anything about the subclass

Alright thanks for the quick answer! (yeah I was thinking it would over ride these two variables but apparently not)

Hey @gitanerie … if the only thing that differentiates one bullet from another is the values of pierce and damage, it sounds like you don’t need to create subclasses? Surely you can just instantiate them with different values? Projectile(Tower fired_from_tower, float x_dep, float y_dep, float direction, float damage, float piercing) ?


Hey !

Well I kind of lied when I said this was the entire subclasses to keep it readable :wink:
There are plenty other variables and sub functions that can be completely different from one projectile to another, so the possibility to over ride the general function would have helped.

But because I couldn’t make the bullet types objects share the same variable, I went with a similar solution (and made different functions as well). To be honest this is what I made in the first place but then I tried to put everything in subclasses because I thought it would take less space in memory, but yeah, not the smartest idea…