#Does this count as a constant?

28 messages · Page 1 of 1 (latest)

shut patio
#

private final double MAX_RANDOM_VALUE = 10.0;

private void lblQuestionComponentShown(java.awt.event.ComponentEvent evt) {     
                                 
     int num1 = (int) (Math.random() * MAX_RANDOM_VALUE) + 1;
int num2 = (int) (Math.random() * MAX_RANDOM_VALUE) + 1;
char operator = getRandomOperator();

String question = "What is " + num1 + " " + operator + " " + num2 + "?";
lblQuestion.setText(question);

int correctAnswer = getCorrectAnswer(num1, num2, operator);
btnSubmit.setActionCommand(String.valueOf(correctAnswer)); // Store the correct answer in the button's action command

// Clear the answer text field
tfieldAnswer.setText("");

}

zenith siloBOT
#

This post has been reserved for your question.

Hey @shut patio! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tired frigate
#

assuming you mean MAX_RANDOM_VALUE, yes, because it has exactly 1 value that it can ever be, and only 1 of it exists.

shut patio
#

thank you

zenith siloBOT
# shut patio thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

opaque portal
tired frigate
#

i just finished exams, i don't have the mental capacity to go through that rn lol. feel free to elaborate on that for them

#

or for me, ig

opaque portal
#

Here MAX_RANDOM_VALUE is final, of primtive type, and initialized at its declaration, with 10.0
10.0 is a literal, that's a constant expression

tired frigate
#

A constant expression is an expression of primitive or String type that is either:
wouldn't a value class also work?

opaque portal
#

That's a literal but not a constant as far as Java defines them

tired frigate
#

wait, it wouldn't be a literal though

opaque portal
#

Ah, sorry. But Java doesn't have value classes yet, does it?

tired frigate
#
public static final Instant DISCORD_EPOCH = Instant.ofEpochSecond(1420070400l);
tired frigate
#

this is referred to as a "constant" in docs as well

opaque portal
#

It's informal anyway, java doesn't have an official concept of that beside preparing for when it will do

tired frigate
#

right, but a correctly-implemented value-based class doesn't really have anything different from a string, no?

#

other than strings being actually special with literals

opaque portal
#

It's just Java does something special for strings, such as definiing them as constants in class bytecode

#

I guess your screenshot demonstrates that even Javadocs don't necessarily mean "compile-time constants in the Java sense" when they use the word "constant"

tired frigate
#

Thinkies i tend to think of anything immutable as capable of being a true constant

opaque portal
#

Which means that the word constant is fairly informal too

tired frigate
#

i guess "constant" in common/general usage would be different to "constant" in jvm/javac implementation, to summarize?

opaque portal
#

pretty much

tired frigate
#

cool