answersLogoWhite

0

Must all final variables be compile time constants?

Updated: 12/24/2022
User Avatar

Anandvijayakumar

Lvl 1
14y ago

Best Answer

No, it is not mandatory. As long as a final variable is initialized only once during the sequence of the code flow, the compiler would not complain.

Ex:

public class Test {

final int x;

public void assignVal(int val){

if(val > 100) {

x = val;

}

}

}

The above code will compile and run just fine as long as there is no other line in the code that assigns a value for x.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Must all final variables be compile time constants?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is declaration of variables allocated memory?

Constants, static variables and global variables are allocated in the program's data segment at compile time. Local variables are allocated on the stack at runtime. Variables cannot be allocated on the heap, you must use a constant, static variable, global variable or local variable to store the start address of a dynamic memory allocation. The variable must be a raw pointer or a reference handle (a smart pointer).


When do you declare a variable method and a class final?

When There is No Need to Change the Values of the Variables In Entire lifetime of That variables then we must use that Variable as Final Variable.


Why can not define private and protected modifiers for variables in interface?

No. Interface variables are supposed to be public static final. Interfaces, like abstract classes, cannot be instantiated, so all variables in an interface must be static final ones. They are public because usually interfaces are used throughout an application, and this will ensure versatility.


Write a program that defines symbolic constants for all of the days of the week in assembly language?

Write a program that defines symbolic constants for all of the days of the week. Create an array variable that uses the symbols as initializers. Comments are a must. For this part, you may not need to put any working code in .code segment. Initialization should be enough. 1. Comments are a must. 2. Plagiarism would automatically qualify for 0. You just have to submit the TWO (2) ".asm" files and they MUST compile


Difference between dynamic and static memory allocation?

Static memory allocation is memory allocated on the "stack" and cannot be resized after the initial allocation, while dynamic memory allocation is memory allocated in the "heap", and can be dynamically expanded and shrunk as necessary.

Related questions

Is declaration of variables allocated memory?

Constants, static variables and global variables are allocated in the program's data segment at compile time. Local variables are allocated on the stack at runtime. Variables cannot be allocated on the heap, you must use a constant, static variable, global variable or local variable to store the start address of a dynamic memory allocation. The variable must be a raw pointer or a reference handle (a smart pointer).


When do you declare a variable method and a class final?

When There is No Need to Change the Values of the Variables In Entire lifetime of That variables then we must use that Variable as Final Variable.


What is the meaning of identifiers in c language?

The NAMES that identify or represent the variables, constants, data types, functions and labels in C language.. They are mere(only) NAMES, that help in IDENTIFYING variables, data types, constants, functions and labels to differentiate them from each other.. A good identifier must be descriptive but short..


What is the difination of linear function?

A linear function is any function that graphs to a straight line. What this means mathematically is that the function has either one or two variables with no exponents or powers. If the function has more variables, the variables must be constants or known variables for the function to remain a linear function.


How do you simplify expessions?

To simplify an expression you must combine the variables and the constants. Then write the simplified version of the expression so that the expression isn't solved, just simplified. 1a+2(a+5)=2a+7


Why can not define private and protected modifiers for variables in interface?

No. Interface variables are supposed to be public static final. Interfaces, like abstract classes, cannot be instantiated, so all variables in an interface must be static final ones. They are public because usually interfaces are used throughout an application, and this will ensure versatility.


Write a program that defines symbolic constants for all of the days of the week in assembly language?

Write a program that defines symbolic constants for all of the days of the week. Create an array variable that uses the symbols as initializers. Comments are a must. For this part, you may not need to put any working code in .code segment. Initialization should be enough. 1. Comments are a must. 2. Plagiarism would automatically qualify for 0. You just have to submit the TWO (2) ".asm" files and they MUST compile


What are the constants of a paper airplane?

fold u must


Difference between dynamic and static memory allocation?

Static memory allocation is memory allocated on the "stack" and cannot be resized after the initial allocation, while dynamic memory allocation is memory allocated in the "heap", and can be dynamically expanded and shrunk as necessary.


Why you have to declare variable first in turbo c?

All variables (and constants) must be declared before they can be used. This is so the compiler knows exactly how much memory to allocate to the variable, as the declaration tells the compiler exactly what the variable's type is.


What is the value of x that makes two lines parallel?

In the standard line equation, y=mx+b, y and x are not constants. They are like the manipulated and responding variables of a science experiment. for two lines to be parallel m must be the same for both lines.


What is the difference between Algebra and calculus?

Algebra must be learned before calculus. Concepts that are learned in algebra are used in calculus, to the extent that a student cannot succeed in calculus unless he knows algebra so well that he does it without thinking.Algebra is the study of constants and variables; that is, it is the study of numbers without knowing specifically what those numbers are.Calculus is the study of rates of change, and is done almost entirely abstractly (without using specific numbers), so it cannot be done without the use of constants and variables (algebra).