-3

I have several variables I need to set only once but never again after starting the application, such as SCREEN_WIDTH, ARRAY_X_LENGTH, TIME_APP_STARTED etc.

I do not declare these final because the software will decide what value to assign to them, not me manually. I capitalize the words though so that I can see that no other code will assign any value to it, only 1.

Is this a bad programming practice, and if yes, what should I do, I usually just have a file containing all these ''final'' variables and my flags, I do not like storing them locally.

Hydraxia
  • 81
  • 2
  • 9
  • 4
    You can actually use `final` and initialize them later, `final` just means they can be initialized _once_ you don't have to do it manually. – Nexevis Aug 02 '19 at 17:05
  • 1
    *the software will decide what value to assign to them* - how? – Thiyagu Aug 02 '19 at 17:05
  • 2
    Having a final variable doesn't mean the value has to be a compile time constant. It can be passed as a constructor parameter *later* – Thiyagu Aug 02 '19 at 17:06
  • 2
    *I capitalize the words though so that I can see that no other code will assign any value to it* - Yes, this is bad. This is just *your* convention. You cannot prevent it from happening – Thiyagu Aug 02 '19 at 17:06
  • 2
    declare final SCREEN_WIDTH, ARRAY_X_LENGTH, TIME_APP_STARTED. once application started assign it once. final variables assignment is allowed once – venkata krishnan Aug 02 '19 at 17:07
  • Possible duplicate of [When is it appropriate to use blank final variables?](https://stackoverflow.com/questions/11345747/when-is-it-appropriate-to-use-blank-final-variables) – Nexevis Aug 02 '19 at 17:08
  • Thanks, I didn't know you could assign values to it while the program is running. This answers the question. And what would you call a variable you assign values to multiple times, but rarely change? – Hydraxia Aug 02 '19 at 17:09
  • 2
    What is `rarely` that is a very generic statement. Sounds like just a variable to me. – Nexevis Aug 02 '19 at 17:10
  • There is no categorization or class of such variables. – Thiyagu Aug 02 '19 at 17:10

2 Answers2

1
public class myClass{
    final int screenWidth;
    final int arrayXLength;
    final int timeAppStarted;

    public myClass(){
        screenWidth = getScreenWidth();
        arrayXLength = arrayX.length;
        timeAppStarted = getTime();
    }
}

Nothing wrong with that - you can just assign them the proper values in your constructor.

Note: These will be assigned a default value so you may want to check whether a proper assignment has been made. You can likewise declare as Integer instead of the primitive data type int.

omoshiroiii
  • 643
  • 5
  • 11
  • really? `final int SCREEN_WIDTH = null;`- 1) `null` cannot be assigned to `int` 2) if it could (e.g assuming `Integer` instead of `int`), this would never be allowed to be changed later on, neither inside the constructor – user85421 Aug 02 '19 at 17:24
  • Woops youre right. Fixed it ;-). – omoshiroiii Aug 02 '19 at 17:28
  • So if I declared `final int SCREEN_WIDTH;` and called its value what would it return, a 0 or a null? – Hydraxia Aug 02 '19 at 17:33
  • 1
    It will return zero as undeclared primitive data types have default values associated with them. To have a null value it must be for an object. To get around this however, you could declare final Integer SCREEN_WIDTH; which would have the effect you are looking for. You are effectively not initializing the variable when you do this. – omoshiroiii Aug 02 '19 at 17:34
  • Thank you and sorry that this question is a duplicate. I actually read that related question before but didn't know it was related because I thought my values weren't considered `final` – Hydraxia Aug 02 '19 at 17:36
  • 1
    Using all capitals for instance fields violates the commonly accepted Java coding standards. – Mark Rotteveel Aug 02 '19 at 17:51
1

Your best bet is to label those variable as final. the final keyword in Java means that the variable can only be initialized once after the application starts.

Note that if you aren't initializing your final variable the moment you're declaring it, then the only places you can initialize them in your code is in static blocks, instance-initializer blocks, or constructors.

Aiyuni
  • 780
  • 5
  • 15