answersLogoWhite

0

What are final variable?

User Avatar

JagritiAgrawal

Lvl 1
13y ago
Updated: 8/20/2019

Declaring a variable with the final keyword makes it impossible to reassign a different value to that variable once it has been initialized with an explicit value (notice we said explicit rather than default). For primitives, this means that once the variable is assigned a value, the value can't be altered. For example, if you assign 10 to the int variable x, then x is going to stay 10, forever. That's pretty straightforward for primitives, but what does it mean to have a final object reference variable? A reference variable marked final can't ever be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed. In other words, if you have a final employee object variable, you can modify aspects of the employee but you cannot have the variable refer to say a manager.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

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 mean final class and final variable?

A final variable cannot have its value changed. It is used to define a constant. Assigning a value to a final variable after it is declared will cause a compiler error. A final class cannot be sub-classed. It is used to prevent rogue programmers from sub-classing your classes and changing their behavior. Trying to extend a final class will cause a compiler error.


What are the difference between private variable and final variable?

A private variable is one that is accessible only to the current class and cannot be accessed by any other class, including the ones that extend from it. A final variable is one that cannot be modified once it is initialized and assigned a value.


What happens if a final keyword is applied to identifier?

The final keyword indicates that a variable (identifier) can not change his value. In case the variable refers to a reference variable (an object) the values of variables inside (the object) can change but the reference can be reassigned (to another object).


Can you use variables in case clauses in the switch statement?

YES - If the variable i a constant (final variable that is already initialized) NO - If the variable is not constant and is assigned at run time.


Can transient variables be declared as 'final' or 'static'?

Transient Variables are those that do not get serialized during the Serialization process. A Static variable is one that is mapped to the Class and is not mapped to any object instance and hence they would not get serialized. So, there should be no problem with declaring a transient variable as static. On the other hand, A final variable is one that's value cannot change. So if you declare a final variable as transient, it would get stored as null during serialization and when you try to de-serialize the variable, you will get errors because, the variable is final and you cannot assign values for it and it is saved as null during serialization. Result: Yes you can have static transient variables but not final transient variables.


Which qualifier is used to declare read-only variable in java?

A variable declared as final can't be modified, once a value is assigned.


what is the difference between final and static keywords?

static: we can use the keyword static either to method or to a variable. when we declare to a method,(eg: public static void main(String args[]),we can use this method without any object. when we use to a variable,there will be only one instance of that variable irrespective of how many objects that get created of that class. Final: Usage of final to method or to a variable makes them as constant. It's value cannot be changed...


How many solutions does an equation have when the variable adds out and the final sentence is false?

none


Why use final in java?

The use of final denotes that a variable is a constant. For example: final int PI = 3.14; makes PI a constant. This is useful because now PI can't be changed, even accidentally, by you, the program, another programmer, or a user. Once a value is final, it is permanent, and can be used as a constant. Note: usually programmers capitalize the names of constants, just to make it clear that the value is a constant.


What is mean by Final?

final is a Java keyword. It is used to:> Mark a variable as final which prevents its value from being changed> Mark a class as final to prevent it from being extended> Mark a method as final to avoid it from beingoverridden


What is public static final variable in java?

-Public to all - Static to access with Class Name - Final to change( constant and not alowed to change) Just use it along with class name. (As implied above, a 'public static final' variable in Java is what other languages would call a Constant. )