# Access sub classes static final variable from superclass

**URL:** https://discourse.processing.org/t/access-sub-classes-static-final-variable-from-superclass/32297
**Category:** Beginners
**Created:** [September 17, 2021, 1:33pm UTC](https://discourse.processing.org/t/access-sub-classes-static-final-variable-from-superclass/32297 "2021-09-17T13:33:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![gitanerie](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@gitanerie](https://discourse.processing.org/u/gitanerie)
#### Post date: [September 17, 2021, 1:33pm UTC](https://discourse.processing.org/t/access-sub-classes-static-final-variable-from-superclass/32297/1 "2021-09-17T13:33:02Z")

</div>

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 :

```auto
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)

Thanks!

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [September 17, 2021, 1:44pm UTC](https://discourse.processing.org/t/access-sub-classes-static-final-variable-from-superclass/32297/2 "2021-09-17T13:44:54Z")

</div>

> [@gitanerie](#):
>
> ```auto
> class Dart extends Projectile{ //this is the entire subclass and other bullet types are equivalent
>   
> Dart(Tower fired_from_tower, float x_dep, float y_dep, float direction){
> super(fired_from_tower, x_dep, y_dep, direction);
> damage = 1;
> pierce = 1;
> } 
> }
> 
> ```

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

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [September 17, 2021, 1:46pm UTC](https://discourse.processing.org/t/access-sub-classes-static-final-variable-from-superclass/32297/3 "2021-09-17T13:46:08Z")

</div>

> [@gitanerie](#):
>
> My question is: is there a way to get the subclasses values from the superclass?

No - the superclass _ **never knows anything** _ about the subclass

---

<div class="post-metadata">

### Author: ![gitanerie](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@gitanerie](https://discourse.processing.org/u/gitanerie)
#### Post date: [September 17, 2021, 2:42pm UTC](https://discourse.processing.org/t/access-sub-classes-static-final-variable-from-superclass/32297/4 "2021-09-17T14:42:55Z")

</div>

> [@quark](#):
>
> simply

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

---

<div class="post-metadata">

### Author: ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)
#### Post date: [September 18, 2021, 8:28pm UTC](https://discourse.processing.org/t/access-sub-classes-static-final-variable-from-superclass/32297/5 "2021-09-18T20:28:30Z")

</div>

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)` ?

---

<div class="post-metadata">

### Author: ![gitanerie](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@gitanerie](https://discourse.processing.org/u/gitanerie)
#### Post date: [September 19, 2021, 12:16pm UTC](https://discourse.processing.org/t/access-sub-classes-static-final-variable-from-superclass/32297/6 "2021-09-19T12:16:46Z")

</div>

> [@rapatski](#):
>
> rely you can ju

Hey !

Well I kind of lied when I said this was the entire subclasses to keep it readable 😉  
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…
