-2

I have a class that has multiple instances of itself, and was wondering if there was a way to override or supress the warning "Variable 'x' might not have been initialized".

class Main{
        public Main(){
            Astronaut.GenerateData(5);
            Astronaut me = new Astronaut();
        }
}

class Astronaut extends Player{
        private static final int HEIGHT;
        public Astronaut(){
            super();
            /*Does stuff*/
        }
        
        public static void GenerateValues(int valueToBeUsed){
            HEIGHT=valueToBeUsed;
        }
    }

I have tried many different options, I know ways around this problem, but overall the code would be cleaner if I can do it this way.

Isaac
  • 29
  • 1
  • 7
  • 3
    "private static final" means you got to but some value in, either at declaration site either in a `static {}` block. This can't be a simple warning. – NoDataFound Dec 07 '22 at 19:33
  • Why is it not intialized? should it be zero? – Cheng Thao Dec 07 '22 at 19:36
  • Are you using an IDE? – WJS Dec 07 '22 at 19:37
  • 4
    This does not look like it should be a static final variable. You will need to think about what static and final mean and whether or not those are appropriate. At a glance, I'd expect that the "height" variable for a player should not be static. – Louis Wasserman Dec 07 '22 at 19:40
  • It should be static for my situation, my code is much more complicated than this, I created a smaller version of the code for simplicity. The code I wrote above is just to demonstrate what issue I am having. – Isaac Dec 07 '22 at 20:03
  • Not a good example then because this means the `HEIGHT` for all Players is exactly the same which does not make any sense – Nexevis Dec 07 '22 at 20:05
  • A static block would be a good idea, except my 'height' value is based on parameters from another class. Is there a way to make that work with a static block? – Isaac Dec 07 '22 at 20:08
  • I updated the code, hopefully it makes more sense to you now. for my case there are multiple subclasses of 'Player' and all 'Astronaut' classes are meant to be the same height – Isaac Dec 07 '22 at 20:11
  • The IDE is android studio – Isaac Dec 07 '22 at 20:15
  • "Why is it not intialized? should it be zero?" I am attempting to initialize it through 'GenerateValues' via class that holds multiple instances of this class. 'GenerateValues' will only be called once for the entire code, so in theory it shouldn't have any problems with it, however the compiler doesn't know this. That's why I'm wondering if theres a way to supress this type of warning. – Isaac Dec 07 '22 at 20:21
  • This is definitely a compile error, not a warning. No suppressing this. Related question is https://stackoverflow.com/q/11345061/217324 – Nathan Hughes Dec 07 '22 at 20:35
  • @NathanHughes I have modified the example above to show the differences with what you sent and what I am trying to do. Are you sure there is no way to override this? I understand why the error is being generated, if there was a way to tell the compiler "GenerateData" is only going to be called once it could potentially ignore this – Isaac Dec 07 '22 at 21:07
  • Perhaps the only way to achieve your goal is to make HEIGHT non-final. – ankit3j Dec 07 '22 at 21:13
  • @ankit3j that is exactly how I had it originally. I wrote this to see if there was a way to override this error, as the variable is being treated as if it was final, and I know that if the warning/error could be ignored it shouldn't generate any issues in run time, but I think you're right. – Isaac Dec 07 '22 at 21:19

1 Answers1

0

First of all, I don't think you should declare HEIGHT as a static var in the Player class. But this might be a test code so that upto you.

Coming to the problem, I am not sure what workarounds you are mentioning but this code is wrong because of the following issues:

  1. A final variable is not assigned default value by the JVM, you need to initialise it with some value.
  2. A static final class variable should be either initialised at declaration or inside a static block.
  private static final int HEIGHT;
  static {
    HEIGHT = 10;
  }
  1. You cannot assign a new value to a final variable. So this HEIGHT=valueToBeUsed; would result in compile error.

Based on the above what you are getting must not be a mere warning but a compile-time error.

ankit3j
  • 51
  • 2
  • 8
  • A static variable can be initialized in a static function, like "GenerateData". The only reason why this does not work is because I have also made it a final variable. Final variables do not need to be declared with initial data if they are initialized in the constructor. I am attempting to initialize it through "GenerateData" instead. The warning comes because the compiler has no way of understanding that "GenerateData" will only be called once, and once initialized you can't change a final variables data, which is why I am looking for a way to override this warning. – Isaac Dec 07 '22 at 20:44
  • Ya I meant static final only. Edited the answer. – ankit3j Dec 07 '22 at 21:04
  • You can assign a variable to a final in the constructor, this is known as a blank final. The error generated is coming up because I am doing it outside of the constructor, however since the function generating it's value is only being used once and before the constructor is used, I was hoping there might be a way for the compiler to take notice of that and ignore the warning/error. – Isaac Dec 07 '22 at 21:25